I want to fill an empty model and save the model in a blob field for later use. My issue is i can not find how to add anouther row to the empty Model.
this works:
$test = LineItem::model();
$test->item_id = '2';
This does not work
$test->1->item_id = '3';
or
$test->item_id[1] = '3';
i have tried looking in the Yii documentation but i was unable to find an answer.
Thanks
Clarification
Im trying to create a false table using the model of a real table. I'm working on an invoicing system and i don't want to right the line items or invoice body information to the DB until it is "closed". Instead i want to fill the corresponding models that will then be serialized and stored in a BLOB field. Once the invoice is finished the data will be written to the table.
You should use
$test = new LineItem;
instead of
$test = LineItem::model();
for INSERT queries. And after setting properties
$test->save();
And so in every iteration.
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
Please be gentle with me - I'm a Laravel noob.
So currently, I loop through a load of users deciding whether I need to update a related model (UserLocation).
I've got as far as creating a UserLocation if it needs creating, and after a bit of fumbling, I've come up with the following;
$coords = $json->features[0]->geometry->coordinates;
$location = new UserLocation(['lat'=>$coords[1],'lng'=>$coords[0]]);
$user->location()->save($location);
My issue is that one the second time around, the Location may want updating and a row will already exist for that user.
Is this handled automatically, or do I need to do something different?
The code reads like it's creating a new row, so wouldn't handle the case of needing to update it?
Update - solution:
Thanks to Matthew, I've come up with the following solution;
$location = UserLocation::firstOrNew(['user_id'=>$user->id]);
$location->user_id = $user->id;
$location->lat = $coords[1];
$location->lng = $coords[0];
$location->save();
You should reference the Laravel API Docs. I don't think they mention these methods in the "regular docs" though so I understand why you may have not seen it.
You can use the models firstOrNew or firstOrCreate methods.
firstOrNew: Get the first record matching the attributes or instantiate
it.
firstOrCreate: Get the first record matching the attributes or create it.
For Example:
$model = SomeModel::firstOrNew(['model_id' => 4]);
In the above example, if a model with a model_id of 4 isn't found then it creates a new instance of SomeModel. Which you can then manipulate and later ->save(). If it is found, it is returned.
You can also use firstOrCreate, which instead of creating a new Model instance would insert the new model into the table immediately.
So in your instance:
$location = UserLocation::firstOrNew(['lat'=>$coords[1],'lng'=>$coords[0]]);
$location will either contain the existing model from the DB or a new instance with the attributes lat and lng set to $coords[1] and $coords[0] respectively, which you can then save or set more attribute values if needed.
Another example:
$location = UserLocation::firstOrCreate(['lat'=>$coords[1],'lng'=>$coords[0]]);
$location will either contain the existing model from the DB or a new model with the attributes set again, except this time the model will have already been written to the table if not found.
<pre>
foreach($cons_name as $name){
$model = new Apps();
$model->const = $name;
$model->value =$value[$i];
$model->save();
}
</pre>
i am using this code how can i add some "where" conditions in this scenario. i am new to yii please tell me how to solve this issue. i am trying to save multiple records with the give code it is working for me but i want to add some more conditions to it.
also tell me how can i add multiple rows at a time instead of using loop
$model = new Apps(); will create a new object for your model. If you want to edit an existing row, you should fetch the corresponding row using find method.
$model = Apps::model()->find('id=1 and type = 2');
As far as I know, you cannot insert multiple rows without using any loop in PHP. What you can do is build you insert query with multiple rows separated by comma and execute it once.
like
INSERT INTO table1 (id,name) VALUES (1,'NAME1'),(2,'NAME2'),(3,'NAME3')
Because you use array with Active Record objects, you need foreach it and set necessary data for each object and then save.
You can use method ActiveRecord.updateAll();
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.
For Yii framework, can we set attribute for save function using object results from find function like below?
$proposal = new Proposals;
$proposal_temp = Proposals_temp::model()->find('eid=?', array($users->eid));
$proposal->Attributes = $proposal_temp;
Would this actually save the Proposal_temp row into Proposals table?
No, you'll have to use the save() method. Attributes is just an array of columns in the table. You'll have to loop through the results of $proposal_temp and save each row. See Documentation