jlorente / yii2-activerecord-inheritance Goto Github PK
View Code? Open in Web Editor NEWActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework
License: MIT License
ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework
License: MIT License
class Parent extends ActiveRecord
{
$test = '123';
public function text()
{
echo $this->test;
}
}
class Child extends ActiveRecord implements ActiveRecordInheritanceInterface
{
use ActiveRecordInheritanceTrait;
$test = '456';
public static function extendsFrom()
{
return Issue::className();
}
}
$child = new Child();
$child->text();
// must be echo '456' but I got '123'
I was write solution, but it's bad solution. Put this into Child class.
public function __call($name, $params) {
try {
return parent::__call($name, $params);
} catch (UnknownMethodException $e) {
foreach (get_object_vars($this) as $cur_obj_attr_name => $cur_obj_val) {
foreach (get_object_vars($this->_parent()) as $parent_obj_attr_name => $parent_obj_val) {
if ($cur_obj_attr_name == $parent_obj_attr_name) {
$this->_parent()->$cur_obj_attr_name = $cur_obj_val;
}
}
}
return call_user_func_array([$this->_parent(), $name], $params);
}
}
Hi!
Is there any way to filter a GridView by parent attributes?
I managed to do the sorting, but the filtering is not working. It's showing validation errors on each field even when no text has been entered.
Thanks in advance
Hello!
First of all thank you for your work!
I found a little issue. Trying explain it :)
If i refresh child model then i see that there are one extra query to parent table. Total 3 queries instead of 2.
I explored the source.
public function refresh() {
$r = parent::refresh();
return $this->_parent()->refresh() && $r;
}
$r = parent::refresh() do 2 queries:
query to child table and extra query to parent, Last query initiate by code:
public static function populateRecord($record, $row) {
parent::populateRecord($record, $row);
_$record->_parent = $record->parent;_
}
Then
$this->_parent()->refresh() && $r do 1 query to parent
Hope you understand my explonation.
I think that should override fields()
and extraFields()
as necessary to serialize data in a RESTFul API.
Hello
Your implementation of populateRecord()
does not allow eager loading.
If we remove this implementation, the Trait continue to operate and increase its performance.
When calling getAttributes() with parameter $names and requesting a field of the child model, an UnknownPropertyException gets thrown, because parameter $names is used unmodified in $this->_parent()->getAttributes($names, $except)
It should be filtered to only contain field names of the parent model.
Last line of getFirstError() should be:
return count($errors) ? $errors[0] : null;
instead of
return empty($errors[$attribute]) ? null : $errors[0];
$errors
is a numeric array.
When trying to access a parent property for a newly created ActiveRecord, the whole server crashes without any error. It's working fine with the 2.0.44 version. I've tried with php 7.4 and 8.0, same results.
This access https://github.com/jlorente/yii2-activerecord-inheritance/blob/master/src/ActiveRecordInheritanceTrait.php#L95 causes the cash. While in the 2.0.44 version it returns null (because the entry is not saved yet) in 2.0.45 it crashes.
Changing the function to this might work:
if ($this->getIsNewRecord() === true) {
$this->_parent = new $pClass();
} else {
$parent = $this->_parent()->one();
if($parent !== null)
$this->_parent = $parent;
}
To reproduce:
$admin = new Admin();
$admin->username = 'al-acran'; <-- crash without any error
This works fine;
$admin = Admin::find()->one();
$admin->username = 'al-acran';
Any thoughts on this?
Hi @jlorente,
Testing your trait with cascading inheritance, getAttributes not work correctly. In this case only returns the values of the attributes of the model and model parent. Grandparent attributes are null.
I propose the following implementation to fix:
public function getAttributes($names = null, $except = []) {
return array_merge($this->_parent()->getAttributes($names, $except), parent::getAttributes($names, $except));
}
regards...
Hi there,
Are the parent rules supposed to be inherited by the child class? I'm assuming so since the properties theirselves are. However that's not working for me.
Thanks
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.