codeigniter-model
codeigniter-model copied to clipboard
Two different arrays for relational models
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();
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());
}
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] => [email protected]
[businessTillnumberId] => 2
[till] => stdClass Object
(
[id] => 2
[till_number] => 255588
)
)
as per your method I will get one array at at time.
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']);
Hi @rupalgohel,
You can also describe how Yii2 query the model array data with relational data, I will check it out.
Another way to get relational data without override:
$activeRecord = $this->Model->findOne(123);
$activeRecord->relationName = $activeRecord->relationName;
print_r($activeRecord->toArray());
@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.
Hi @rupalgohel,
Good point!
I will add Eager Loading feature (with
method) to the milestone.
Thank for your advice.
Thanks for consideration @yidas. I will use this in my new CI projects.
@yidas , I am waiting for this.
@yidas , any update?
@yidas, I am waiting for this too
Dear @yidas any update for eager loading? Maybe this will help:
- https://kevdees.com/raw-php-and-mysql-eager-loading/#:~:text=Eager%20loading%20is%20merely%20fetching,used%20by%20those%20authors%20beforehand.
- https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
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!