Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Two different arrays for relational models #16

Open
rupalgohel opened this issue Mar 18, 2019 · 13 comments
Open

Two different arrays for relational models #16

rupalgohel opened this issue Mar 18, 2019 · 13 comments
Milestone

Comments

@rupalgohel
Copy link

rupalgohel commented Mar 18, 2019

I am getting two different arrays for relational model.
Like Model - 1:
[_readProperties:yidas\Model:private] => Array
(
[id] => 1
[value] => 545488
[data] => 1
)
with this $vendors = $model->vendors; I am getting another data.
Here model 1 hasOne relation with vendor table.
My requirement and yii2 also gives relational query data in single array
(
[id] => 1
[value] => 545488
[data] => 1
[verndor] => ()
)
and how to get array of query $model = $this->Vendor_model->findAll();

@rupalgohel rupalgohel changed the title Two different arrays for relation model Two different arrays for relational model Mar 18, 2019
@rupalgohel rupalgohel changed the title Two different arrays for relational model Two different arrays for relational models Mar 18, 2019
@yidas
Copy link
Owner

yidas commented Mar 18, 2019

Hi @rupalgohel,

findAll() will return an array of model objects:

// Find the active records whose primary key value is 10, 11 or 12.
$activeRecords = $this->Model->findAll([10, 11, 12]);

// Find the active recordd whose type is 'A' and whose status is 1
$activeRecords = $this->Model->findAll(['type' => 'A', 'status' => 1]);

// Query builder ORM usage
$this->Model->find()->where_in('id', [10, 11, 12]);
$activeRecords = $this->Model->findAll();

// Print all properties for each active record from array
foreach ($activeRecords as $activeRecord) {
    print_r($activeRecord->toArray());
}

@rupalgohel
Copy link
Author

rupalgohel commented Mar 18, 2019

Hii @yidas , Thanks for quick reply.
after converting to array I will not have data of relational model
I would need data as per below. where [till] is relational method name.
like in vendor model
$this->has_one['till'] = ['Till_model','id','businessTillnumberId'];

(
            [id] => 1
            [name] => Vendor1dsfsdf
            [emailId] => vendor1@g.com
            [businessTillnumberId] => 2
            [till] => stdClass Object
                (
                    [id] => 2
                    [till_number] => 255588
                )

        )

as per your method I will get one array at at time.

@razorsharpshady
Copy link

razorsharpshady commented Mar 18, 2019

Hi @rupalgohel ,
To get the relational data as well in model array, I think you should override the toArray() in your model class something like this:

public function toArray($relationNames =[]){
        $data = parent::toArray();
        foreach ($relationNames as $relationName){
            try{
                $data[$relationName] =  $this->$relationName;
            }catch(\Exception $e){}
        }
        return $data;
    }

and then use it like this

$data = $model->toArray(['till,'bill','mill']);

@yidas
Copy link
Owner

yidas commented Mar 19, 2019

Hi @rupalgohel,

You can also describe how Yii2 query the model array data with relational data, I will check it out.

@yidas
Copy link
Owner

yidas commented Mar 19, 2019

Another way to get relational data without override:

$activeRecord = $this->Model->findOne(123);
$activeRecord->relationName = $activeRecord->relationName;
print_r($activeRecord->toArray());

@rupalgohel
Copy link
Author

@razorsharpshady, thanks for your suggestion.
I guess, loop for getting relational model data to combine with the base model that won't be the appropriate way, correct me if I understand it wrongly.

@yidas , as per your suggested code, what if I want all the active records with relational model.

Yes we can also implement the way Yii2 gives like

Till::find()->joinwith('relationalModelName')->all();

This will give output as I mention above without use of for loop or someother.

@yidas
Copy link
Owner

yidas commented Mar 19, 2019

Hi @rupalgohel,

Good point!
I will add Eager Loading feature (with method) to the milestone.

Thank for your advice.

@yidas yidas added this to the Eager Loads milestone Mar 19, 2019
@rupalgohel
Copy link
Author

Thanks for consideration @yidas. I will use this in my new CI projects.

@rupsk1607
Copy link

@yidas , I am waiting for this.

@Zubair-Iftikhar
Copy link

@yidas , any update?

@eraporsmk
Copy link

@yidas, I am waiting for this too

@masarbi
Copy link

masarbi commented Jun 5, 2021

@yidas
Copy link
Owner

yidas commented Jun 5, 2021

Hi~

It seems that I was developing this before, but then I thought that there are not many users of this package now:
https://github.com/yidas/codeigniter-model/tree/dev_eager

If anyone else is eager for this feature, please star this project and you could also comment here to let me know~

Thank you for your support!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants