Git Product home page Git Product logo

yii2-activerecord-inheritance's Issues

Was not overridden parent attributes while calling parent method

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);
        }
    }

Filter child model by parent attributes

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

An extra query to the database on model.refresh()

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.

Eager loading support

Hello
Your implementation of populateRecord() does not allow eager loading.
If we remove this implementation, the Trait continue to operate and increase its performance.

getAttributes throws UnknownPropertyException

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.

Bug in getFirstError

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.

Not compatible with yii2 2.0.45

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?

getAttributes not work correctly with multiple levels of inheritance

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...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.