<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();
Related
I have a situation to update the row based on id and return the complete row. I can do it in two queries but i want to do it in one query only. I have write this...
$result= DB::table($this->table)
->whereRaw($where['rawQuery'], $where['bindParams'] ? $where['bindParams'] : array())
->increment($updateField);
if($result){
return DB::table($updateTable)
->select('id')
->where('campaign_id',$where['bindParams'])
->where('date',date("Y-m-d"))
->get();
}else{
throw Exception("Error in fetching data");
}
I copied this from what you commented in your question:
No i want to return the id of the updated row or complete row if possible
If you want to return the ID of the just updated 'row' you're talking about. You can use Eloquent to accomplish this.
You can use a route like this:
Route::put('demo/{id}', Controller#update);
Then in your update function in your controller you can get the ID.
Find the model with $model = Model::find(id);
do things with the data you get.
Like $model->name = Input::get('name');
Then use $model->save();
After saving you can do $model->id;
Now you get back the ID about the row you just updated.
Refer back to this question:
Laravel, get last insert id using Eloquent
But any way it'll always be at least 2 queries (a SELECT and an UPDATE in MySQL, however you do it)
You can check Laravel Eloquent if you want a "cleaner" way to to this.
my application needs a custom tagging system. This means creating multiple records for each new tag, and then adding the id's of those records to a pivot table.
I can insert multiple records from an array using Laravels "insert":
$tag_titles = explode(",", $tag_titles);
foreach ($tag_titles as $tag_title) {
$tags[] = array('tag_title' => $tag_title);
}
$tag = Tag::insert( $tags );
but now i need to know the id's that were created during the insert. Is there a way of doing this without making multiple calls to the DB to do each insert separately?
Thanks.
so far I have tried
sync() method will only work for a single id :
$tag->content()->sync($tags);
After the insert, $tag is not an object, so $tag->id won't work
It looks like you're using a QueryBuilder instance to insert, in which case, you should be able to use insertGetId
https://laravel.com/api/5.1/Illuminate/Database/Query/Builder.html#method_insertGetId
i've built an correct working query with the query builder.
But now, there is a condition, where a method (that is responsible to dynamically ad some tables to the query) must add a columns to the query.
i've tried the following (it's much more complex, but its almost the same):
$querybuilder->select('EntityA.Property')
->from ('EntityA');
// here is happening some awesome stuff... ;-)
// Now i have to add the Table, and The column
$querybuilder->innerJoin('EntityB'); // this is working
$querybuilder->add('select', 'EntityB.Property'); // overwrites my columnlist
// $querybuilder->select('EntityB.Property'); // also overwrites my columnlist
thanks in advance
Why do you not just assemble the select-clause seperately like this:
$fields = array();
$fields[] = "EntityA.Property"
// code here, and finally, you decide you need EntityB
$fields[] = "EntityB.Property"
$querybuilder->innerJoin('EntityB');
// done with building the query, assign select
$querybuilder->select($fields);
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.
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