Related
I am creating a custom Identity interface without created_at property. I got an error :
"name": "Unknown Property",
"message": "Setting unknown property: api\\common\\models\\User::created_at",
I tried to comment the TimestampBehavior, but I got the following error:
"name": "PHP Warning",
"message": "Invalid argument supplied for foreach()",
I want to know where is the problem.
Model class:
class User extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return '{{%user}}';
}
public function behaviors()
{
// return [
// TimestampBehavior::className(),
// ];
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['purpose'], 'required'],
[['status'], 'integer'],
];
}
}
for the rest controller the action is
public function actionLogin(){
.
.
.
$api_user = new User();
$api_user->purpose="app";
$api_user->status=User::STATUS_ACTIVE;
if($api_user->save()){
$success = true;
}
}
This will automatically resolve the issue. BlameableBehavior and TimestampBehavior
// Include these on the start
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use Carbon\Carbon;
// Paste this function inside the class.
/**
* #return array
*/
public function behaviors()
{
return [
'blameable' => [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'created_by',
'updatedByAttribute' => 'updated_by',
],
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => Carbon::now(),
],
];
}
NOTE: If you are not using updated_at or updated_by then remove it
form the above code
change your Behavior in your model to:
public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
'value' => new Expression('NOW()'),
],
];
}
if you haven't updated_at also delete it from attributes.
You were getting following warning because you've completely removed the return in the behaviors() method.
"name": "PHP Warning",
"message": "Invalid argument supplied for foreach()",
The behaviors method must return an array. If you don't want to use any behavior your behaviors() method should return empty array like this:
public function behaviors()
{
return [];
}
This is also default implementation of behaviors() method in yii\base\Component so if you don't need to use any behavior you can simply remove the behaviors() method from your model.
Attaching TimestampBehavior to your model when you are not using it means that you add unnecessary overhead.
Example: Rename and prevent time recording or remove properties. Also change the value
Rename or delete properties or change value.
public function behaviors()
{
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
// 'createdAtAttribute' => 'c_time', //Change the name of the field
'updatedAtAttribute' => false, //false if you do not want to record the creation time.
// 'value' => new Expression('NOW()'), // Change the value
],
];
}
Or
'class' => \yii\behaviors\TimestampBehavior::className(),
'attributes' => [
\yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
// \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => [],
],
$createdAtAttribute: The attribute that will receive timestamp value Set this property to false if you do not want to record the creation time.
$attributes: List of attributes that are to be automatically filled with the value specified via $value. The array keys are the ActiveRecord events upon which the attributes are to be updated, and the array values are the corresponding attribute(s) to be updated. You can use a string to represent a single attribute, or an array to represent a list of attributes. For example,
[
ActiveRecord::EVENT_BEFORE_INSERT => ['attribute1', 'attribute2'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2',
]
In a BI project we have multiple reporter functionalities. So, we have defined some classes to implement this feature. This classes needs many attributes to chain and build complex queries to generate reports. Any other classes should set specific values for these attributes, to get reports from this class. Values of these attributes are Non-Dynamic. I don't use the database to store them.
Below codes are the current model i am using:
Report generator (Main class):
class Report
{
private $indicator;
private $ratio;
private $divider;
private $criteria;
private $plantation;
private $reporter;
public function reporter($reporter)
{
$this->reporter = (new Reporters())->get($reporter);
return $this;
}
public function plantation($plantationId)
{
$this->plantation = $plantationId;
return $this;
}
public function ratio($ratio)
{
$this->ratio = (new Ratios())->get($ratio);
return $this;
}
public function divider($divider)
{
$this->divider = (new Dividers())->get($divider);
return $this;
}
public function criteria($criteria)
{
$this->criteria = $criteria;
return $this;
}
public function get()
{
return $this->mocker();
}
}
Dividers Class:
class Dividers
{
public $dividers = [
'sum' => [
'name' => 'مجموع',
'alias' => 'sum',
],
'plantations' => [
'name' => 'مجموعه انتخابی',
'alias' => 'plantations',
'model' => Plantation::class
],
'operation_types' => [
'name' => 'نوع عملیات',
'alias' => 'operation_type',
'model' => OperationType::class
],
'planting_years' => [
'name' => 'سال زراعی',
'alias' => 'planting_years',
'model' => Planting_year::class
],
'crops' => [
'name' => 'انواع گیاهان',
'alias' => 'crops',
'model' => Crop::class
],
];
public function get($divider)
{
if(!array_key_exists($divider, $this->dividers)){
return false;
}
return $this->dividers[$divider];
}
}
Ratio Class:
class Ratios
{
public $ratios = [
'SUM' => 'انباشته',
'KILOGRAM' => 'کیلوگرم',
'HECTARE' => 'هکتار',
'RIALPERKILO' => 'ریال به کیلوگرم',
'MILIONRIALPERTON' => 'میلیون ریال بر تن',
];
public function get($ratio)
{
if(!array_key_exists($ratio, $this->ratios)){
return false;
}
return $this->ratios[$ratio];
}
}
So for using report generator i will use this method:
$report = (new Report())
->plantation(352)
->divider('sum')
->reporter('NetProfit', ['operation_type'=> 364])
->criteria([['criteriaType'=> 'human_resources', 'value'=> '256'],['criteriaType'=> 'human_resources', 'value'=> '326']])
->ratio('sum')
->indicator(324, 523, 632)
->get();
My question is: what is the best pattern to store this data objects to reduce human mistakes?
This is more of an opinion based answer, so I'll suggest what I do when I am using static values.
Declare class members as static and protected for variables.
Like in your question class Ratios { } is a static class
class Ratios{
protected static $ratios = [...];
public static function get($ratio)
{
if(!array_key_exists($ratio, self::$ratios)){
return false;
}
return self::$ratios[$ratio];
}
}
//Access the Ratios class with.
$val = Ratios::get($ratio);
This ensures that
the values won't change throughout the lifecycle of your request
Adds a layer of security.
Also maintains the source, i.e. no change will occur if you don't change the code.
Doesn't create a new Instance(new Ratios()) for getting static values and you have that memory edge.
Do the same with the class Dividers { }.
I don't know if this is the best practice, but i would make a separate directory called "constants" or "config" or something that seems intuitive to you, and add there files named like Class_Property.php that return the value of that property
For example, in you Ratios class:
class Ratios
{
public $ratios = require('config/Ratios_ratios.php');
public function get($ratio)
{
if(!array_key_exists($ratio, $this->ratios)){
return false;
}
return $this->ratios[$ratio];
}
}
And in config/Ratios_ratios.php:
<?php
return [
'SUM' => 'انباشته',
'KILOGRAM' => 'کیلوگرم',
'HECTARE' => 'هکتار',
'RIALPERKILO' => 'ریال به کیلوگرم',
'MILIONRIALPERTON' => 'میلیون ریال بر تن',
];
?>
Depending on how critical that data is, opt for require/require_once/include. This is done mainly to keep your class more skinny, separating the constants
I'm trying to save multiple records via
AppSettings::create(
[
'name' => 'mail_host',
'type' => $emailsettingstype->id,
'value' => '',
],
[
'name' => 'mail_port',
'type' => $emailsettingstype->id,
'value' => '',
],
[
'name' => 'mail_username',
'type' => $emailsettingstype->id,
'value' => '',
],
);
But from the above, only the first array is getting created. Where am i going wrong? Any help is appreciated.
I think this should do
AppSettings::createMany([
[
'name'=>'mail_host',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_port',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_username',
'type'=>$emailsettingstype->id,
'value'=>'',
],
]);
Make sure you're passing an array of arrays, not a params of array.
UPDATE, you can use Model::insert() although according to what I've read, that method doesn't create/update the timestamps.
You can just use Eloquent::insert() link as below:
AppSettings::insert([
[
'name'=>'mail_host',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_port',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_username',
'type'=>$emailsettingstype->id,
'value'=>'',
],
]);
The problem with above is that it won't update timestamps, find examples here
The Create many Method createMany is available on relationship check reference to this link and this documentation from laravel
so far my example look like this.
I have two models Pricing and AvailableService Model
Pricing Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pricing extends Model
{
protected $fillable = ["name", "price"];
public function available(){
return $this->hasMany(AvailableService::class, "pricing_id", "id");
}
}
And the AvailableServiceMode look like this
namespace App;
use Illuminate\Database\Eloquent\Model;
class AvailableService extends Model
{
protected $fillable = ["pricing_id", "service_id"];
public function service(){
return $this->belongsTo(Service::class, "service_id", "id");
}
}
So createMany operation look like this
$insertMany = Pricing::create(['name'=>request('name')]);
$insertMany->available()->createMany([
['service_id'=>1],
['service_id'=>2],
['service_id'=>3],
['service_id'=>4],
['service_id'=>5],
]);
And it works for, you can give it a try too. THANKS
If you want to store multiple record in seeder use this method instead of insert because in my case I want to slug automatically created using spatie/laravel-sluggable pkg. If you used the insert or DB technique then you have to give the value for slug field also.
CategorySeeder
<?php
namespace Database\Seeders;
use App\Servcategory;
use Illuminate\Database\Seeder;
class CategorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$categories = [
[
'name' => 'Automotive',
// 'slug' => 'automotive',
],
[
'name' => 'Business Services',
// 'slug' => 'business-services',
],
[
'name' => 'Computer, Telecom & IT Services',
// 'slug' => 'computer-telecom-&-it-services',
],
[
'name' => 'Education & Training',
// 'slug' => 'education-&-training',
],
[
'name' => 'Finance',
// 'slug' => 'finance',
],
[
'name' => 'Hospitals, Clinic, Medical',
// 'slug' => 'hospitals-clinic-medical',
],
[
'name' => 'Real Estate, Construction, Property',
// 'slug' => 'real-estate-construction-property',
],
[
'name' => 'Travel,Toursim & Hotels',
// 'slug' => 'travel-toursim-&-hotels',
],
];
// Servcategory::insert($categories);
collect($categories)->each(function ($category) { Servcategory::create($category); });
}
}
In case some one searching for eloquent model, I used the following method:
foreach($arCategories as $v)
{
if($v>0){
$obj = new Self(); // this is to have new instance of own
$obj->page_id = $page_id;
$obj->category_id = $v;
$obj->save();
}
}
$obj = new Self(); is a must otherwise it only saves single record when $this is used.
in seeder create an array and do foreach with Model::create(). All your records will be with timestamps
protected $array = [
[...],
[...],
[...]
];
public function run()
{
foreach ($this->array as $value) {
Model::create($value);
}
}
I want to realize a form, which is quite simple. The only thing that makes things complicated is that I 'm using two collections in my form. Displaying two collections in the view works like a charme. The problem is the validation and the associated hydration of the bound entity of the form. If all is validated and no errors occur the form instance tries to hydrate the bound entity and ends up with an exception:
Zend\Hydrator\ArraySerializable::hydrate expects the provided object to implement exchangeArray() or populate()
But first the example code ...
The form classes
namespace Application\Form;
use Zend\Form\Element\Collection;
use Zend\Form\Element\Text;
use Zend\Form\Form;
class MyForm extends Form
{
public function __construct($name = '', $options = [])
{
parent::__construct($name, $options);
$this->setAttribute('method', 'post');
$this->setAttribute('id', 'my-form');
}
public function init()
{
$this->add([
'name' => 'my-text-field',
'type' => Text::class,
'attributes' => [
...
],
'options' => [
...
],
]);
// The first collection
$this->add([
'name' => 'first-collection',
'type' => Collection::class,
'options' => [
'count' => 2,
'should_create_template' => true,
'template_placeholder' => '__index__',
'allow_add' => true,
'allow_remove' => true,
'target_element' => [
'type' => FieldsetOne::class,
],
],
]);
// the second collection
$this->add([
'name' => 'second-collection',
'type' => Collection::class,
'options' => [
'count' => 2,
'should_create_template' => true,
'template_placeholder' => '__index__',
'allow_add' => true,
'allow_remove' => true,
'target_element' => [
'type' => FieldsetTwo::class,
],
],
]);
}
}
The metioned Fieldset classes which are bound to the collections look pretty much the same.
namespace Application\Form;
use Zend\Form\Element\Number;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class FieldsetOne extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add([
'name' => 'my-number',
'type' => Number::class,
'options' => [
...
],
'attributes' => [
...
],
]);
}
public function getInputFilterSpecification()
{
return [
'my-number' => [
'required' => true,
'filters' => [
[
'name' => StripTags::class,
],
[
'name' => ToInt::class,
],
],
'validators' => [
[
'name' => NotEmpty::class,
],
[
'name' => IsInt::class,
'options' => [
'locale' => 'de_DE',
],
],
],
],
];
}
}
Summed up the form got two collections of number elements. All data which is provided over the form should end up in the following entity.
The input filter class
The form gets filtered and validated by the following input filter. The input filter will be bound to the form via a factory. The factory will be shown later.
class MyFormInputFilter extends InputFilter
{
public function init()
{
$this->add([
'name' => 'my-text-field',
'required' => true,
'filters' => [
[
'name' => StripTags::class,
],
[
'name' => StringTrim::class,
],
],
]);
}
}
The input filter contains only settings for the my-text-field element. The collections will be validated with the implemented InputFilterProviderInterface in the fieldsets set as target elements. The input filter class is created over a factory and notated in the input_filters section in the module.config.php.
The form entity
The entity will be bound as an object to the form in a factory it looks like the following example.
namespace Application\Entity;
class MyFormEntity
{
protected $myTextField;
protected $firstCollection;
protected $secondCollection;
public function getMyTextField()
{
return $this->myTextField;
}
public function setMyTextField($myTextField)
{
$this->myTextField = $myTextField;
return $this;
}
public function getFirstCollection()
{
return $this->firstCollection;
}
public function setFirstCollection(array $firstCollection)
{
$this->firstCollection = $firstCollection;
return $this;
}
public function getSecondCollection()
{
return $this->secondCollection;
}
public function setSecondCollection(array $secondCollection)
{
$this->secondCollection = $secondCollection;
return $this;
}
}
This entity will be bound as object to the form. The form will be hydrated be zend 's own ClassMethods hydrator class. For the collections two hydrator strategies are added to the hydrator. The hydrator strategies for the collections look like this.
namespace Application\Hydrator\Strategy;
class FirstCollectionStrategy extends DefaultStrategy
{
public function hydrate($value)
{
$aEntities = [];
if (is_array($value)) {
foreach ($value as $key => $data) {
$aEntities[] = (new ClassMethods(false))->hydrate($data, new CollectionOneEntity());
}
}
return $aEntities;
}
}
This strategy will hydrate the data from collection one to the corresponding entity.
All wrapped up in a factory
This is the factory which creates the form instance.
class MyFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();
$filter = $parentLocator->get('InputFilterManager')->get(MyFormInputFilter::class);
$hydrator = (new ClassMethods())
->addStrategy('first-collection', new FirstCollectionStrategy())
->addStrategy('second-collection', new SecondCollectionStrategy());
$object = new MyFormEntity();
$form = (new MyForm())
->setInputFilter($filter)
->setHydrator($hydrator)
->setObject($object);
return $form;
}
}
This factory is mentionend in the form_elements section in the module.config.php file.
The problem
Everything works fine. The input element and also the collections are rendered in the view. If the form is submitted and the $form->isValid() method gets called in the controller all ends up in a BadMethodCallException.
Zend\Hydrator\ArraySerializable::hydrate expects the provided object to implement exchangeArray() or populate()
I have not bound the collection entities to the form in the controller because the hydrator strategies are added to the form hydrator that should hydrate the form entity. This makes sense for me, because zend form can only bind one object. If i call the bind method twice in the controller, the first bound object will be overwritten.
Is it possible to add more than one object with the bind method of the form so two collections can be handled? What could alternatives look like? What I 'm doing wrong?
In Yii2 I have blog with articles, each one has 1 category. Rules are so:
'rules' => [
'<category_slug>/<article_slug>' => 'article/view',
],
controller:
public function actionView($category_slug, $article_slug)
{
...
}
Categories are stored in params.php:
<?php
return [
'categories' => [
[
'id' => 1,
'name' => 'First category',
'slug' => 'first_category',
],
...
],
];
Url to article:
Url::to(['article/view', 'category_slug' => $model->category_slug, 'article_slug' => $model->article_slug])
Question: Is it possible to auto prepend category slug to Url::to? I mean, you need only make Url::to with article_slug param. I suppose best solution will be to change url rules somehow, but how exactly?
'rules' => [
'<Yii::$app->MyFunction->GetCategorySlugByArcticleId(..)>/<article_slug>' => 'article/view',
],
This should be an easy one.
Option one: extend the Url-Helper
Simply extend the Url-Helper as follows:
class MyUrl extends \yii\helpers\Url
{
//extending regular `to`-method
public static function to($url = '', $scheme = false)
{
//check if url needs completion and meets preconditions
if (is_array($url) && strcasecmp($url[0], 'article/view') === 0 && isset($url['article_slug']) && !isset($url['category_slug'])) {
//add category slug if missing...assuming you have a relation from article to category and category having a property `slug`
$url['category_slug'] = $article->category->slug;
}
return parent::to($url, $scheme);
}
//...or custom method for your case which is even more convenient
public static function toArticle($article, $scheme=false)
{
//article could be an article model itself or an id
$articleModel = $article instanceof Article ? $article : Article::findOne($article);
return parent::to([
'article/view',
'article_slug'=>$articleModel->slug,
'category_slug'=>$articleModel->category->slug,
], $scheme);
}
}
All you have to do now is use that class instead of the regular Url-helper!
Option two: add a method within your article model
Simply add a method as follows:
class Article extends \yii\db\ActiveRecord
{
//...
public function link($scheme=false)
{
return Url::to([
'article/view',
'article_slug'=>$this->slug,
'category_slug'=>$this->category->slug,
], $scheme);
}
//...
}
Let me know if you need more info!