yii2tech / ar-linkmany Goto Github PK
View Code? Open in Web Editor NEWActiveRecord behavior for saving many-to-many relations
License: Other
ActiveRecord behavior for saving many-to-many relations
License: Other
I tried to add this behavior for link between tables group
and category
using pivot table category_group
that have columns group_id
and category_id
.
When I added behavior and set up "virtual property" groupsIds
, I tried to execute request with adding one category with three records from group to be related with: groupsIds => [1,2,3]
As expected from link method, I got three MySQL queries:
INSERT INTO `category_group` (`category_id`, `group_id`) VALUES (6, 1);
INSERT INTO `category_group` (`category_id`, `group_id`) VALUES (6, 2);
INSERT INTO `category_group` (`category_id`, `group_id`) VALUES (6, 3);
So I think it's better to use one query:
INSERT INTO `category_group` (`category_id`, `group_id`) VALUES (6, 1), (6, 2), (6, 3);
What do you think about enhancement? Is it needed or no?
Could extraColumns
be specified in the reference attribute?
$model->reference = [1 => ['extra' => 'value'], 2, 3 => ['extra' => 'other']];
This would be a good way to update any extra columns too where the keys stay the same.
When I tried to use delete method for the model with defined behavior, it's relations didn't delete. Maybe it could be useful to add EVENT_AFTER_DELETE handler and unlink all related records.
Is there a way to implement the feature? I'd like to set some attrubutes depends on user group. For example, only Admin can change groupIds.
I tried
public function rules() { return [ [['!groupIds'], 'safe'], ]; }
controller:
$post = Yii::$app->request->post(); if (Yii::$app->user->hasRole('admin') && isset($post[$model->formName()]['groupIds'])) { $model->groupIds = $post[$model->formName()]['groupIds']; }
but it has no effect.
After
$model->load($post)
anyone can change the attribute
For example: order has products and clients. How to link two relations?
I need to get id of related model to set the value of my ExtraColumn.
Like this:
'extraColumns' => [
'option_id' => function($model) {
return isset($this->optionsIds[$model->id]) ? $this->optionsIds[$model->id] : null;
}
]
I will create pull request for it.
Hi,
If the contents of the relationReferenceAttribute array changes, then this attribute does not fit in dirtyAttributes, so they do not work out behaviors like TimestampBehavior, BlameableBehavior and others.
(\yii\behaviors\AttributeBehavior::evaluateAttributes)
How to be in this situation?
Hi
is there a way to trigger Events on each link / unlink the models?
e.g. if i use the behavior to save the relation between a submission and a reviewer.
So after an allocation of an reviewer an email should be sent out to this newly linked reviewer.
Also some email should be sent on unlink.
Currently I only see the possibility of saving the value before and after Update and get the newly linked and relations to remove myself and use this information to create the emails.
But maybe you got some better suggestion?
Another question is there a better way to have blameable columns filled with the correct information?
Currently i fill those fields like following:
'extraColumns' => [
'reviewer_type' => self::REVIEW_TYPE_INVIVIDUAL,
'created_at' => function () {
return Carbon::now('UTC')->toDateTimeString();
},
'updated_at' => function () {
return Carbon::now('UTC')->toDateTimeString();
},
'created_by' => function () {
return Yii::$app->user->id;
},
'updated_by' => function () {
return Yii::$app->user->id;
},
],
best regards
I have many-to-many links. I want to sort them. Can use extra field for this? I write extra field when creating a connection. But how can update extra field for exist links? Please any example.
links TABLE actor_movie (
actor_id int
movie_id int
movie_ord int
)
Hi,
Love this behavior !
Would it be possible to add a property to set the delete
flag when calling unlink
?
When used on a many-to-many relation with a pivot table, I need to delete entries, not to set it to null.
Thanks
@klimov-paul Павел, добрый день!
ar-linkmany/LinkManyBehavior.php
Line 96 in 391dc0f
В данной строке бехейвера принудительное привидение аттрибута к массиву из-за этого невозможно провалидировать массив данных( массив связывания таблиц - "groupIds") через EachValildator так, как внутри валидатора в цикле аттрибуту (groupIds) присваивается значение каждого эллемента массива.
When I set validation rule:
['groupIds', 'each', 'rule' => ['integer']
always get validation error ''{attribute} must be an integer.''
Problem in line LinkManyBehavior.php#L98
Steps to reproduce the problem:
'linkServicesBehavior' => [
'class' => LinkManyBehavior::className(),
'relation' => 'salons',
'relationReferenceAttribute' => 'salonsIds',
],
safe
attribute_form.php
The problem is:
Invalid argument supplied for foreach()
in LinkManyBehavior.php:245
Need to check if $this->owner->{$this->relation}
isset.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.