The following code will return an array of PHP Activerecord Objects:
$book = Book::find('all');
Assuming the program is aware of the order of books I can continue and update the attributes of the books and save them to the database as follows:
$book[0]->title = 'my first book';
$book[0]->author = 'Danny DeVito';
$book[4]->title = 'Nice Title';
in order to save the above I would have to invoke the ->save() method on each object
$book[0]->save();
$book[4]->save();
Is there a better way to do this? built-in PHP ActiveRecord function
that saves all members of a given array of objects, or based on an
association?
Assuming the original title of $book[4] above was already 'Nice
Title', would the ->save() method consider $book[4]changed and
continue with the database save?
Try using update all insted
$update = array();
$update['title'] = 'my first book';
$update['author'] = 'Danny DeVito' ;
$book[0]->update_all(array('set' =>$update));
$book[4]->update_all(array('set' =>array("title"=>"Nice Title"));
I think this should be cleaner
After much research I decided to post my conclusions/answers:
There is no such ActiveRecord library function that can update an
array of objects with unique values.
Assuming Activerecord would shoot one update request it would look like this:
UPDATE books
SET title = CASE id
WHEN 0 THEN 'my first book'
WHEN 4 THEN 'Nice Title'
END,
author = CASE id
WHEN 0 THEN 'Danny DeVito'
END
WHERE id IN (0,4)
The same question as "how would I update multiple rows with different values at once". This would go against the design of an Activerecord model, as an Object represents a row, and maps rows across tables. An obvious limitation for having such an easy model to work with.
Any assignment to an Object's attributes triggers a 'dirty' flag on
that attribute, and any subsequent call to update/save that
object will trigger a query even if the assigned attribute value is
the same as the database/model's previous value. Invoking the
save() method when no assignments were made does not trigger this
query.
Related
Code:
$item1 = Item::find(1);
$item1->foo = 1;
$item1->save();
$another_item1 = Item::find(1);
dd($another_item1->foo);//Is this value always 1?
My question:
Is always read the newly written data after calling save() method of ORM? In my example, Is $another_item1->foo always 1?
If the answer to question 1 is not, how could I ensure I read the newly written data from the database?
Is always read the newly written data after calling save() method of ORM?
No, there is no SELECT ran after a INSERT or UPDATE statement in this case.
In my example, Is $another_item1->foo always 1?
Based on your own comment, Yes.
If the answer to question 1 is not, how could I ensure I read the newly written data from the database?
$model->save();
// Reload the current model instance with fresh attributes from the database.
$model->refresh();
// OR
// Reload a fresh model instance from the database.
$fresh = $model->fresh();
I think you may be confused about the find() function. find() is used to fetch one or many models by its / their primary key(s). The return value will either be a single model, a collection or null if the record is not found.
If you are looking to lookup multiple rows you need to run Item::get();
Uses
$Item = Item::find(1); // returns model or null
$Items = Item::find(array(1, 2, 3)); // returns selected Items in collection
$Items = Item::get(); // Returns all in collection
https://laravel.com/docs/5.5/eloquent
i have an issue with access to the id of the ActiveRecord model in Yii2 framework. when i save the model i just created, i cannot acquire the id field of new object.
$house = new House;
$house->save();
$hid = $house->id;
$hid value is empty string ''.
the problem is that i am creating new model, so that i can pass the new id to thread process that is handling file moving while i create db rows. thread starts and after json slicing and array populating, the first insert fail on sql condition (where) statement.
i have researched many answers and they point to several flaws:
assignment of pk - i don't assign the new model id field (db handles the pk autoincrement), i receive the $_post body content through json (json has many fields that are not for bulk assignment into main model, so i deal with slicing the json data before $attibutes insert).
pk in model rules - i don't have id field in model rules array.
fault in the ActiveRecord class - i don't want to hack the base classes of the framework.
later in code i planned to link the models through relations, but i suppose that failed because of this error, so i also use $hid value to populate the foreign key fields in related models.
help. please.
Could be a validation problem try in this way
$house = new House;
if ($house->validate()) {
$house->save();
$hid = $house->id;
} else {
$errors = $house->errors;
var_dump($errors)
}
If you see the result of var_dump the your validation fails (eg: some required fields .. ) and you need change the proper validation rules in your House Model ..
Otherwise you cant try with
$house->save(false); //this way the validation is not executed
(use save(false) only for debugging porpose)
thank you scaisEdge and Alex. i have forgot to check the not null columns of the db. yii2 gii module generated the model according to the db schema and i missed the rules fields of the model. i didn't need validation since i was just generating empty row (just pk).
this is the code that passes:
$house = new House;
$house->name = 'name'; [field set as required in model rules array]
$house->description = 'description'; [field set as required in model rules array]
$house->save();
$hid = $house->id;
convention and configuration, pretty neat.
I want to set a certain attribute in all the models of a collection.
in plain SQL:
UPDATE table SET att = 'foo' WHERE id in (1,2,3)
the code i have:
$models = MyModel::findMany([1,2,3]);
$models->update(['att'=>'foo']);
taken from here
but doesn't work. I'm getting
Call to undefined method Illuminate\Database\Eloquent\Collection::update()
the only way i have found it's building a query with the query builder but i'd rather avoid that.
You are returning a collection, not keeping the query open to update. Like your example is doing.
$models = MyModel::whereIn('id',[1,2,3]);
$models->update(['att'=>'foo']);
whereIn will query a column in your case id, the second parameter is an array of the ids you want to return, but will not execute the query. The findMany you were using was executing it thus returning a Collection of models.
If you need to get the model to use for something else you can do $collection = $models->get(); and it will return a collection of the models.
If you do not just simply write it on one line like so;
MyModel::whereIn('id',[1,2,3])->update(['att'=>'foo']);
Another option which i do not recommend is using the following;
$models = MyModel::findMany([1,2,3]);
$models->each(function ($item){
$item->update(['att'=>'foo']);
});
This will loop over all the items in the collection and update them individually. But I recommend the whereIn method.
The best solution in one single query is still:
MyModel::whereIn('id',[1,2,3])->update(['att'=>'foo']);
If you already have a collection of models and you want to do a direct update you can use modelKeys() method. Consider that after making this update your $models collection remains outdated and you may need to refresh it:
MyModel::whereIn('id', $models->modelKeys())->update(['att'=>'foo']);
$models = MyModel::findMany($models->modelKeys());
The next example I will not recommend because for every item of your $models collection a new extra query is performed:
$models->each(function ($item) {
$item->update(['att'=>'foo']);
});
or simpler, from Laravel 5.4 you can do $models->each->update(['att'=>'foo']);
However, the last example (and only the last) is good when you want to trigger some model events like saving, saved, updating, updated. Other presented solutions are touching direct the database but models are not waked up.
Just use the following:
MyModel::query()->update([
"att" => "foo"
]);
Be mindful that batch updating models won't fire callback updating and updated events. If you need those to be fired, you have to execute each update separately, for example like so (assuming $models is a collection of models):
$models->each(fn($model) => $model->update(['att'=>'foo']) );
I find very annoying to have to fetch an object by id from database every time I want to add it to a relationship. Is there a way to add an object to a a relationship by id instead of adding the whole object?
This is my actual code:
...
$person = $em->getRepository("Person")->findOneById($id);
$me->getPersons()->add($person);
...
I would like to have something like this:
...
$me->getPersons()->add($id);
...
Then I would be saving one trip to the database! Which I like better! Is it possible?
You don't have to do that actually. You can get reference object like so:
$person = $em->getReference("Person", $id);
$me->getPersons()->add($person);
Doctrine will not make a query for Person but will instead return an reference proxy object for person with that id. If you, however do:
$person = $em->getReference("Person", $id); // 0 queries
$person->getId(); // Still 0 queries
$person->getSomeField(); // query fired
Doctrine will trigger lazy load if you try to get some field that has to be fetched from database.
See docsEntityManager::getReference method
I need to just get the first record from a Yii CActiveRecord derived class. In Rails I would just be able to do this:
post = Post.first
I thought I could do the same thing with Yii like this:
$post = Post::model()->first();
But that method doesn't exist. Do I have to just do find with a condition to get the first record?
I don't see first() in the docs for CActiveRecord so I assume the answer is no, it doesn't have a first method. So how would one go about querying just the first record?
This works but sure is an ugly hack. Surely there's a better way.
$first = Post::model()->findAll(array('order'=>id, 'limit'=>1));
Yii isn't going to make any assumptions about how your data should be ordered. Good database design requires that if you use a surrogate key, that key should have a meaningless value. That means NOT using it for ordering.
That issue aside, here is probably the best way to do your query:
$first = Post::model()->find(array('order'=>'id ASC'));
By using find instead of findAll you automatically apply a LIMIT 1 to your result. I would not skip the inclusion of the order by clause, as that insures that the database will order the results consistently.
If you use this query a lot, you can create the following method. UPDATE: Modified it to throw an exception when the primaryKey is composite or missing. We could add more error checking as well, but we leave that as an exercise for the reader. ;)
public function first($orderBy = null){
if(!$orderBy){
$orderBy = self::model()->tableSchema->primaryKey;
}
if(!is_string($orderBy)){
throw new CException('Order by statement must be a string.');
}
return self::model()->find(array('order'=>$orderBy));
}
Then include this method in a class which extends CActiveRecord, and then extend all your models form that class.
The wrapper I wrote will by default order results by the primary key, but you could optionally pass a different column and direction (ASC OR DESC) if you wish.
Then if you do this for the post class, you can access the first model like so:
$first = Post::model()->first();
CActiveRecord::find() returns only one model.
$first=Post::model()->find();
Yii2 asks for a condition when doing a findOne().
You could do a find() following with no conditions and just return one()
$first= Post::find()->one();
To really be sure you could just add a orderBy clause to it:
$first= Post::find()->orderBy(['id' => SORT_ASC])->one();
Same goes for the command function:
$first= \Yii::$app->myDatabase->createCommand('SELECT * FROM Post ORDER BY id ASC')->queryOne();