Kohana 3.2 ORM multiple records update - kohana

How can I update multiple records in Kohana 3.2's ORM?
For example this:
$menu = ORM::factory('menu');
$menu->where('active','=',1);
$menu->active=2;
$menu->save();
does not work, it inserts a new record.
Thanks

If you don't want to hardcode the table name maybe something like below
DB::update(ORM::factory('menu')->table_name())
->set(array('active' => '2'))
->where('active', '=', '1')
->execute();

Related

Find all collections in mongodb with specific field

There is more than 40 collections in database I am currently working on.
One of the major key in all the collections is "account".
I need to know all such collections where there is a field called "account".
Is there a query to get or a js script which prints all such collections?
In Oracle I was using :
SELECT * FROM ALL_TAB_COLUMNS WHERE COLUMN_NAME LIKE 'account';
Any inputs is helpful.
Thanks in advance.
The following mongo script will print out all collection names where at least one document contains an account field.
db.getCollectionNames().forEach(function(collname) {
var count = db[collname].find({"account": {$exists: true}}).count();
if (count > 0) {
print(collname);
}
})

Yii Lazy Loading From Object Instance ($this) with Criteria (Is it possible?)

I'm new to Yii and used to Kohana. I have two models:
1. sampleRequests -> (this has many SampleShares)
2. sampleShares
In my case I have already instantiated an object (sampleRequest) and I am trying to get a particular sampleShares based on added criteria.
In Kohana I could do:
$myShare = $this->sampleShares->where('user_id','=',$my_user_id)->find();
It Yii I have tried something similar:
$myShare = $this->sampleShares->find(array(
'condition'=>'user_id:user_id',
'params'=>array(':user_id'=>$my_user_id)));
The previous does not work for me however, the following will work:
$myShare = SampleShare::model()->find(array(
'condition'=>'sample_id=:id AND user_id=:user_id',
'params'=>array(':id'=>$this->id, ':user_id'=>Yii::app()->user->id)));
So my question is, can you grab related models with appended select criteria to the relationships established in Yii (particular with the instance or $this)? (Similar to my Koahana example?) If so how do you do that?
$myShare = SampleShare::model()->findAll(array('condition'=>'sample_id='.$id.' AND user_id='.$user_id));
and relation query example
$myShare = SampleShare::model()->with('sampleRequests')->findAll(array('condition'=>'sample_id='.$id.' AND user_id='.$user_id));
you can find the realtion name in your model file...
And then you can get the related table records using $myShare->sampleRequests
you can set relationship between the tables in your model file, like this:
public function relations()
{
return array(
'category' => array(self::BELONGS_TO, 'CategoryCategory', 'category_id'),
);
}
how to use:
$model=Blog::model()->with('category')->findAll(); //object
Nevermind I figured it out. I just had to replace:
$this->sampleShares->find(
with
$this->with('sampleShares')->find(

Kohana 3 cross-table update using Query builder

What is the proper way to construct a cross-table update in Kohana 3 using the DB query builder?
Currently I am just using a DB::expr but I know the query builder is smarter than that.
// update record
$rows_updated = DB::update(DB::expr('user_list_permissions INNER JOIN users ON user_list_permissions.user_id = users.id'))
->set($params)
->where('user_list_permissions.id', '=', $user_list_permission_id)
->where('users.account_id', '=', $this->account_id)
->execute();
And yes of course I tried using the "join" method like when building SELECT queries, but I receive an error:
ErrorException [ 1 ]: Call to undefined method Database_Query_Builder_Update::join()
So you are using the expression to do the join, it is possible to use the built in 'join' function on the 'on' function to achieve this behavior.
So in your example it would look something like:
$rows_updated = DB::update('user_list_permissions')
->join('users','INNER')
->on('user_list_permissions.user_id','=','users.id')
->set($params)
->where('user_list_permissions.id', '=', $user_list_permission_id)
->where('users.account_id', '=', $this->account_id)
->execute();
There isn't much on it but the docs do have a little bit at http://kohanaframework.org/3.2/guide/database/query/builder#joins
It's an old post but just to keep the record of my experiences in Kohana.
If you are using MySQL, it lets you to make the cross-table update avoiding the use of join as follow:
UPDATE table1, table2
SET table1.some_field = 'some_value'
WHERE table1.foreign_key = table2.primary_key AND table2.other_field = 'other_value'
Note that condition table1.foreign_key = table2.primary_key is the same you used in ON clause with JOIN. So you can write a cross-table update in Kohana follow that pattern avoiding the JOIN:
$rows_updated = DB::update(DB::expr('user_list_permissions, users'))
->set($params)
->where('user_list_permissions.user_id', '=', DB::expr('users.id'))
->where('user_list_permissions.id', '=', $user_list_permission_id)
->where('users.account_id', '=', $this->account_id)
->execute();

Kohana 3.2 ORM Does not contain model info

I'm working with Kohana 3.2 and have the following code in my controller.
$Blog_Post = new Model_Blogpost();
$Blog_Post->where('id', '=', 1);
$Blog_Post->find();
$content = $Blog_Post->content;
I Currently have 3 records in my db with id's 1, 2, and 3.
$Blog_Post->content, or any other field return null. and I'm not sure why.
Use ORM::factory('blogpost', $id) or new Model_Blogpost($id) if you need an object with PK == $id.
Check your model after loading.
if $Blog_Post->loaded()
{
// it works!
}
else
{
// record not found
}
If record not found, you can see last DB query with $Blog_Post->last_query()
UPD. From comments. Your model will not work with this modifications. Note that ORM data stored in $_object property, and $Blog_Post->content is just a shortcut for $Blog_Post->_object['content'] via __get() method. Of course, if you define public $content property, $Blog_Post->content will return NULL value instead of using DB data.
There is no reason for defining model fields as properties. If you need IDE hints, just use PHPDOC.
At the firm I work for we were looking into upgrading to 3.2 very recently. However, in our evaluation I don't recall seeing a difference in ORM handling methods.
Yours above looks like it should be something like this:
$Blog_Post = ORM::factory('blogpost')->where('id', '=', 1)->find();
$content = $Blog_Post->content;
Assuming your table is called blogposts, of course. I may be wrong about that and if I am, can you link to the documentation that shows this kind of model interaction?

How would I change this Query builder from Kohana 2.4 to Kohana 3?

I have had this website built for a few months and I am just getting on Kohana 3. I'd just like to convert this K2.4 query builder to K3 query builder.
return DB::select(array('posts.id', 'posts.created', 'posts.uri', 'posts.price', 'posts.description', 'posts.title',
'image_count' => db::expr('COUNT(images.id)')))
->from('posts')
->join('images')->on('images.post_id', '=', 'posts.id')
->group_by(array('posts.id'))
->order_by('posts.id', 'DESC')
->limit($limit)
->offset($offset)
->execute();
The only change you need to make, is drop the surrounding array from the DB::select(), and for the aliased field, use an array
The query builder in Kohana3 accepts any number of arguments, see http://kohanaframework.org/guide/database/query/builder
return DB::select('posts.id', 'posts.created', 'posts.uri',
'posts.price', 'posts.description', 'posts.title',
array('COUNT("images.id")', 'image_count'))
->from('posts')
->join('images')->on('images.post_id', '=', 'posts.id')
->group_by(array('posts.id'))
->order_by('posts.id', 'DESC')
->limit($limit)
->offset($offset)
->execute();

Resources