Processing function in Yii2 Kartik GridView property - yii2

In my Kartik GridView viewfile, I am attempting to process a function for the detailRowCssClass property of an ExpandRowColumn. Regardless of setup, (such as applying an empty function or returning direct strings), the result is always the same and an object is returned.
'detailRowCssClass' => function($data){
if($data->status == 0)
{
return GridView::TYPE_INFO;
}
elseif($data->status == 1)
{
return GridView::TYPE_WARNING;
}
elseif($data->status == 2)
{
return GridView::TYPE_SUCCESS;
}
},
returns a class of [object Object]
Does anyone know a workaround, or what I am fundamentally missing in that this does not return a string? Thanks!

The problem is, that detailRowCssClass of the class kartik\grid\ExpandRowColumn is a simple string and not a closure. The appropriate parts from the source file vendor/kartik-v/yii2-grid/src/ExpandRowColumn.php:
class ExpandRowColumn extends DataColumn
{
...
/**
* #var string the CSS class for the detail content table row.
*/
public $detailRowCssClass;
...
/**
* #inheritdoc
* #throws InvalidConfigException
*/
public function init()
{
if (!isset($this->detailRowCssClass)) {
$this->detailRowCssClass = $this->grid->getCssClass(GridView::BS_TABLE_INFO);
}
...
$clientOptions = Json::encode(
[
'gridId' => $this->grid->options['id'],
'hiddenFromExport' => $this->hiddenFromExport,
'detailUrl' => empty($this->detailUrl) ? '' : $this->detailUrl,
'onDetailLoaded' => $onDetailLoaded,
'expandIcon' => $this->expandIcon,
'collapseIcon' => $this->collapseIcon,
'expandTitle' => $this->expandTitle,
'collapseTitle' => $this->collapseTitle,
'expandAllTitle' => $this->expandAllTitle,
'collapseAllTitle' => $this->collapseAllTitle,
'rowCssClass' => $this->detailRowCssClass,
'animationDuration' => $this->detailAnimationDuration,
'expandOneOnly' => $this->expandOneOnly,
'enableRowClick' => $this->enableRowClick,
'enableCache' => $this->enableCache,
'rowClickExcludedTags' => array_map('strtoupper', $this->rowClickExcludedTags),
'collapseAll' => false,
'expandAll' => false,
'extraData' => $this->extraData,
]
);
...
}

Related

Cannot generate HalResource for object of type ArrayObject

I've some problems to return a paginator object as HAL json collection. I'm using the latest versions of zend-expressive and zend-expressive-hal.
This is the setting from my ConfigProvider:
public function __invoke() : array
{
return [
'dependencies' => $this->getDependencies(),
MetadataMap::class => $this->getHalConfig(),
];
}
public function getHalConfig() : array
{
return [
[
'__class__' => RouteBasedCollectionMetadata::class,
'collection_class' => RoleCollection::class,
'collection_relation' => 'user_roles',
'route' => 'api.user.roles',
],
];
}
And these are my handler methods:
public function get(ServerRequestInterface $request) : ResponseInterface
{
// read some records from the database
$select = new Select();
$select->from(['r' => 'user_roles']);
$select->columns(['id', 'name']);
$paginator = new RoleCollection(new DbSelect($select, $this->dbAdapter));
$paginator->setItemCountPerPage(25);
$paginator->setCurrentPageNumber(1);
return $this->createResponse($request, $paginator);
}
private function createResponse(ServerRequestInterface $request, $instance) : ResponseInterface
{
return $this->responseFactory->createResponse(
$request,
$this->resourceGenerator->fromObject($instance, $request)
);
}
The RoleCollection class is only an inheritance of the Paginator:
class RoleCollection extends Paginator
{
}
The error message which I get is:
Cannot generate Zend\Expressive\Hal\HalResource for object of type ArrayObject; not in metadata map
I think you are missing the metadata for the Role object itself.
For example this is something similar for my posts object:
MetadataMap::class => [
[
'__class__' => RouteBasedCollectionMetadata::class,
'collection_class' => Posts::class,
'collection_relation' => 'posts',
'route' => 'api.posts',
],
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => Post::class,
'route' => 'api.posts.view',
'extractor' => ArraySerializable::class,
],
],
You have only described the collection and the resource class is missing for a single role.
I also see the resource generator tries to parse an ArrayObject. This should be wrapped in a Role object, which you can add to the MetadataMap.
Where it goes wrong in your code is this line:
$paginator = new RoleCollection(new DbSelect($select, $this->dbAdapter));
This adds the result of a query into the paginator, but the paginator does not know how to handle it. If I remember correctly, the DbSelect return a ResultSet. I'm guessing this is where the ArrayObject is coming from. What you probably need is to override that ResultSet and make sure it returns an array of Role objects. You might want to look into the dbselect adapter and the hydrating resultset.
Once you have the Role object in the paginator, you can describe it in the metadata.
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => UserRole::class,
'route' => 'api.roles',
'extractor' => ...,
],
I use doctrine myself with hal so zend-db is out of my scope. If you need more help, I suggest the zf forums.

How to insert same data record to multiple table on Yii2

I'm using yii2-advanced. I've several table :
tb_user:(iduser(PK),username),
tb_profile:(id,iduser(FK)),
tb_status:(id,iduser(FK))
My question is how can i insert iduser(PK) from tb_user to iduser(FK) on tb_profile and tb_status after i push the signup button.
For a while i think i must to do some modification of bevahiours() function on User model and i found some error, or adding trigger syntax on the table ? (i think this is not a good ways).
Is there anyone who can help me, how to solve my problem ?
this is the User model before the modification :
<?php
namespace common\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
/**
* #inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],
'value' => function () {return date('Y-m-d h:m:s');},
],
];
}
/**
* #inheritdoc
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
];
}
/**
* #inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
/**
* #inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}
}
?>
The Controller :
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
I had similar situation in one of my project where i had 2 tables like user,user_image where user_id was foreign key to add the path.
For those kind of situation you can use either of following approach
1.Insert record in both table on click of signup button. You will have to write update action accordingly.
$user = new User();
$user->name = "John"
$user->email = "John#gmail.com"
//Add if any other fields in table
$user->save(); //save the record
$user_image = new UserImage();
$user_image->user_id = $user->id;
$user_image->image = "image path"
//Add any other images here
$user_image->save();//save the record
2.You can also call create action of UserImage and do the same. If you use this approach than you might also need to use any other unique column to find the id of that user and use it to insert new record,for example in my table email is unique column so i can write following code in UserImage and get the id
$user = User::findOne(['email' => 'john#gmail.com']);//this will return whole row
$user_image->user_id = $user->id;
$user_image->image = "image path"
//Add any other images here
$user_image->save();//save the record
And that way you can use the code as per it suits your need
Thank you

how to define a relationship in Yii2 with orOnCondition

I am logging changes in my database to a table called audit_field. For a given model I would like to retrieve all the audit_fields for this model as well as some of the related models.
For example:
<?php
class Job extends ActiveRecord
{
public function getAuditFields()
{
$link = []; // what do I put here to get "1=1" ?
return $this->hasMany(AuditField::className(), $link)
->orOnCondition([
'audit_field.model_id' => $this->job_id,
'audit_field.model_name' => get_class($this),
])
->orOnCondition([
'audit_field.model_id' => ArrayHelper::map($this->getJobTeches()->all(), 'id', 'id'),
'audit_field.model_name' => 'app\models\JobTech',
]);
}
public function getJobTeches()
{
return $this->hasMany(JobTech::className(), ['job_id' => 'job_id']);
}
}
What I want:
SELECT * FROM audit_field WHERE (...my or conditions...);
// or
SELECT * FROM audit_field WHERE (1=1) AND (...my or conditions...);
What I get:
SELECT * FROM audit_field WHERE (1=0) AND (...my or conditions...)
Found the answer, don't use hasMany, instead just return an ActiveQuery:
<?php
class Job extends ActiveRecord
{
public function getAuditFields()
{
return AuditField::find()
->orOnCondition([
'audit_field.model_id' => $this->job_id,
'audit_field.model_name' => get_class($this),
])
->orOnCondition([
'audit_field.model_id' => ArrayHelper::map($this->getJobTeches()->all(), 'id', 'id'),
'audit_field.model_name' => 'app\models\JobTech',
]);
}
}

Cannot redeclare class Cake\ORM\Behavior\ in CakePHP 3

I created src/Model/Behavior/ModernTreeBehavior.php:
<?php
namespace Cake\ORM\Behavior;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use InvalidArgumentException;
use RuntimeException;
class ModernTreeBehavior extends Behavior
{
/**
* Cached copy of the first column in a table's primary key.
*
* #var string
*/
protected $_primaryKey;
/**
* Default config
*
* These are merged with user-provided configuration when the behavior is used.
*
* #var array
*/
protected $_defaultConfig = [
'implementedFinders' => [
'children' => 'findChildren',
'treeList' => 'findTreeList'
],
'implementedMethods' => [
'childCount' => 'childCount',
'getLevel' => 'getLevel'
],
'parent' => 'parent_id',
'path' => 'path',
'scope' => null,
'level' => null
];
public function getLevel($entity)
{
$primaryKey = $this->_getPrimaryKey();
$id = $entity;
if ($entity instanceof EntityInterface) {
$id = $entity->get($primaryKey);
}
$config = $this->config();
$entity = $this->_table->find('all')
->select([$config['path']])
->where([$primaryKey => $id])
->first();
if ($entity === null) {
return false;
}
return substr_count($entity[$config['path']], '-') + 1;
}
/**
* Returns a single string value representing the primary key of the attached table
*
* #return string
*/
protected function _getPrimaryKey()
{
if (!$this->_primaryKey) {
$this->_primaryKey = (array)$this->_table->primaryKey();
$this->_primaryKey = $this->_primaryKey[0];
}
return $this->_primaryKey;
}
}
I used $this->addBehavior('ModernTree'); in CommentTables.php
and I got error Fatal error: Cannot redeclare class Cake\ORM\Behavior\ModernTreeBehavior in .../src/Model/Behavior/ModernTreeBehavior.php on line 0
But if I paste /src/Model/Behavior/ModernTreeBehavior.php into built-in file TreeBehavior.php and load TreeBehavior.php, everything so goods.
Can you tell me the reason?
The namespace declaration of your class is wrong, it should be:
namespace App\Model\Behavior;

Symfony2: How to render datetime-local fields with FormBuilder?

The title says it all: How can i render html5 datetime-local fields from the formbuilder?
With
$builder->add('start', 'datetime', array('widget' => 'single_text'))
i can get a datetime field, but this is not supported by chrome.
Thanks in advance!
This is an old question, but for anybody coming across this post searching for a solution to that issue (like me), here is how I solved it:
Add a custom field type that inherits DateTimeType:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LocalDateTimeType extends AbstractType
{
const HTML5_FORMAT = "yyyy-MM-dd'T'HH:mm";
/**
* {#inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['widget'] = $options['widget'];
if ($options['html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
$view->vars['type'] = 'datetime-local';
}
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'format' => self::HTML5_FORMAT
]);
}
/**
* {#inheritdoc}
*/
public function getParent()
{
return 'datetime';
}
/**
* #return string
*/
public function getName()
{
return 'local_datetime';
}
}
Register it as a service:
app.form.type.local_datetime:
class: AppBundle\Form\Type\LocalDateTimeType
tags:
- { name: form.type, alias: local_datetime }
Now you may use it in your forms:
(...)
$builder->add('myLocalDateTimeField', 'local_datetime', [
'widget' => 'single_text'
];
(...)
It's important that you set the widget to single_text - and yes, this is of course possible (as of Symfony 2.7).
Using localized dateTimes, you'll probably want to store your datetimes normalized in the database. A nice approach can be found here:
http://jeremycook.ca/2012/06/24/normalising-datetimes-with-doctrine-events/
The widget 'single_text' is not supported by the datetime field type.
The date and time parts have independent widgets. For Example:
<?php
// from date
$builder->add('DateFrom', 'datetime', array(
'input' => 'datetime',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'empty_value' => '',
'format' => 'yyyy-MM-dd',
'label' => 'From Date',
'required' => false,
'mapped' => false
));
Definition of the widget options for the date type.
Definition of the widget options for the time type.