Yii Framework Forum: An object instead of an array - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

An object instead of an array

#1 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 08 December 2011 - 12:13 AM

This topic just gave me an idea. Sometimes I have to write such code in my models:

protected function beforeDelete()
{
    if (parent::beforeDelete())
    {
        foreach ($this->someRelation as $model)
            $model->delete();
        return true;
    }
}

Here I manually delete all related models, because for each of them I need before/after delete methods to be called. But what about a more simple solution:

$this->someRelation->delete();

This is HAS_MANY relation and now it returns an object instead of an array (but can be also used as an array).
Same can be done for findAll() methods:

MyModel::model()->findAll($criteria)->delete();

There can be other methods besides delete(). What do you think about it? :)
2

#2 User is offline   Pravin Gajera 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 137
  • Joined: 25-October 11
  • Location:India

Posted 08 December 2011 - 02:01 AM

delete specific records without pk

$criteria=new CDbCriteria;
$criteria->condition='field1=:field1 AND field2=:field2';
$criteria->params=array(':field1'=>$field1,':field2' =>$field3 );
Model_Name::model()->deleteAll($criteria);
0

#3 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 640
  • Joined: 23-October 10
  • Location:Romania

Posted 22 December 2011 - 07:47 AM

I find this very interesting.
Usually, InnoDB does this job for me, BUT there are cases when, beside deleting the actual record from the database, i also need to delete something else (that is not connected to that particular model via innodb relations).

The most common example is:
Say i have a table User and a Post table.
Each user may have n posts, but a post can also have a "main image", which is a file on the server and a column in the database table(not blob, just plain varchar, holding the file path).

Now, using innodb, when deleting a certain user, automatically all his posts are deleted too, but what about the images of the posts ? These will be orphaned on the server.
Even if the post model has before/after delete methods(and in those methods i delete the after mentioned file), these methods won't be called in this case.

So, i end up with:
foreach($user->posts AS $post)
{
  $post->delete();//to call the before/after delete methods and get rid of the files.
}
$user->delete();


So i think we will need some kind of idea so that if the above scenario applies, the before/after methods of the Post model are called too.

Thoughts ?
0

#4 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 22 December 2011 - 08:03 AM

I like the idea
findAll and has-many / many-many relations could return a CModelCollection object that extends from ArrayIterator
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#5 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 01 January 2012 - 01:28 PM

In extention to Gustavo, the collection class returned could be configurable in the relation? So $someGallery->images could return my own ImageCollection?
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 01 January 2012 - 02:11 PM

View PostThomas Jensen, on 01 January 2012 - 01:28 PM, said:

In extention to Gustavo, the collection class returned could be configurable in the relation? So $someGallery->images could return my own ImageCollection?

Would be cool to write something like this? :)

$gallery->images->addWatermark('watermark.png');

0

#7 User is offline   Rangel Reale 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 14-January 10

Posted 08 January 2012 - 01:11 PM

View PostThomas Jensen, on 01 January 2012 - 01:28 PM, said:

In extention to Gustavo, the collection class returned could be configurable in the relation? So $someGallery->images could return my own ImageCollection?


I do exactly this on my custom Yii version, all relations returns a CModelCollection (that is a CModel in with all "fields" are a number-based array index), and the relation can be configured to use a different CModelCollection-descending class. This is a very powerful concept.

For example, a model collection that knows how to save many_many relations, you just add a checkbox list directly to the "groups" field, and it gets the value array and saves on the relation table:

	public function relations()
	{
		return array(
			'groups'=>array(self::MANY_MANY, 'AdminGroup', '{{admin_groups}}(admin_id, admingroup_id)',
				'with'=>'operations',
				'modelCollection'=>array('wobjext.wyii-ext.modelcollection.WManyManyModelCollection')),
		);
	}


1

#8 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 19 January 2012 - 02:53 AM

Can we have a core developer's thoughts on this?
0

#9 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,615
  • Joined: 17-January 09
  • Location:Russia

Posted 21 January 2012 - 03:46 PM

Thinking about having a collection object for a long time already but have no final decision on how its API should look like and if we should implement it at all. It certainly can be useful but most of the time there's no need to use it.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#10 User is offline   Ulten 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 04-October 10

Posted 29 January 2012 - 05:01 AM

having Yii arrays as silver bullet for any static values is minefield with hi risks in it. Hi coupling possible compatibility issue in future. Is it so hard, create static objects with need parameters for all classes that use static words for config.

Plus side of it will be available and documented all variants of settings and you font have to search 'showHeader' parameter name means, or even exists.

pray to Yii2 will have it.
0

#11 User is offline   phpnode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 141
  • Joined: 18-April 11

Posted 21 February 2012 - 01:50 PM

I really like the idea of relation lists being objects, it would also be very cool to be able to do something like:
$model->myHasManyOrManyManyRelation->getDataProvider(); // get an active data provider with the related records
$model->myHasManyOrManyManyRelation->add($otherModel); // add relation from model instance
$model->myHasManyOrManyManyRelation->add(123); // add relation by id
$model->myHasManyOrManyManyRelation->remove(123); // remove relation by id
$model->myHasManyOrManyManyRelation = array(123, 456, $otherModel); // massively assign the relations, replacing existing assignments
$model->myHasOneRelation = 123; // update the foreign key in the appropriate field in row 123 of the remote table
$model->myHasOneRelation = $otherModel; // update the foreign key in the appropriate field of $otherModel
$model->myBelongsToRelation = 123; // set the appropriate foreign key in $model to 123
$model->myBelongsToRelation = $otherModel; // update the appropriate foreign key in $model to the pk of $otherModel

0

#12 User is offline   JulianRutten 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 08-September 11
  • Location:Noord-Brabant, The Netherlands

Posted 28 March 2012 - 09:25 AM

I agree, the idea of a 'object collection' class is nice. I often find myself either having to create one to handle my Has_many relations, especially when you for example got multiple attributes each having their own (multiple) values. This gets messy without a decent collection class because of the need to add extended functionality or exceptions.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users