Status update: ActiveRecord

Is there a way we can configure stored procedures operations for the ones we want?

Right now, I’m not using them because of lack of support from Yii but have used them extensively in the past.

I have used iBatis (now MyBatis) in Java to create the data mappings between everthing.

I know it’s more work but also, its more powerful.

MyBatis

To support rawtaz on the requirement for relational savings:

UI elements like the following should be able to use this. Those are very common elements to use in your app, which shouldnt require the current complexities of 1.1 to save.

CHtml::activeCheckBoxList()

CHtml::activeDropDownList()

CHtml::activeListBox()

CHtml::activeRadioButtonList()

Also, some extensions like with-related-behavior delete all MANY_MANY relations in the in-between-table, and then remake them. I know there won’t be MANY_MANY relations in v2, but I just want to point out that such a behavior isn’t optimal.

I am adressing these problems in my Extension ActiveRecordRelationBehavior, which might be of interest for you and others who need the comfort of saving related records directly with save(). This one keeps entries in MANY_MANY table btw.

If I may ask, what is the advantage of dropping the MANY_MANY type of relation?

Jeroendh

  1. It was introduced when there was no through option for relations that’s much more flexible. In Yii2 it’s called via, has significantly better syntax and is generally simpler to define.

  2. If you’re going not only to display data but to add it, you need model for the “middle” table anyway.

  3. If you’re going to have data in the “middle” table, you can’t use MANY_MANY to get this data.

  1. Makes it easy to have a model for your "middle" table.

samdark, thank you for your insight.

What I do in Yii1 is just make two relationships. One hasmany to the middle table, and a manymany. I suppose we simplify things by only having two types of relationships.

If I can make another few questions:

  1. Shouldnt Order have many orderItems in the example, instead of have one?



'orderItems:OrderItem[]' => array(    //added []

        'link' => array('order_id' => 'id'),

),



  1. Why do we need to setup a link in the items relation? Isnt that set in the OrderItem class?

That way it could be:




   // via another relation

   'items:Item[]' => array(

        'via' => 'orderItems.item',     //links are set in orderItems relation and in the item relation of the OrderItem class

   ),

  1. The @'s in the relations of the Order model confuse me. @ is the self table, right? ie, tbl_order. Should it be ‘order’ => ‘?.id’ and ‘on’ => ‘?.category_id = 1’ then?

  2. To make things even more readable, what about adding the ‘via’ prequel as well? All links seem to be “That foreign attribute refers to this attribute of myself.”, but that makes the link in the via-table relation look odd/unlogical.


class Order extends ActiveRecord

{

        public static function tableName()

        {

                return 'tbl_order';

        }


        public static function relations()

        {

                return array(

                        'customer:Customer' => array(

                                'link' => array('id' => 'customer_id'),

                        ),

                        'orderItems:OrderItem[]' => array(

                                'link' => array('order_id' => 'id'),

                        ),

                        // via another relation

                        'items:Item[]' => array(

                                'via' => 'orderItems.item',

                                'order' => '?.id',

                        ),

                        // via a join table

                        'books:Item[]' => array(

                                'joinType' => 'INNER JOIN',

                                'via' => array(

                                        'table' => 'tbl_order_item',

                                        'link' => array(

                                                'order_id' => 'id',

                                        ),

                                ),

                                'link' => array(

                                        'id' => 'via.item_id',  //added via., because item_id isnt an attribute of Order

                                ),

                                'on' => '?.category_id = 1',

                        ),

                );

        }

}

Maybe I need more coffee and Im making a fool of myself. Sorry in that case. :)

  1. orderItems is the name of relation, OrderItem is the model we’re binding to, [] stays for “many”.

  2. To execute less code and in order for the code to be explicit. For example, you can have two different relations between two model using two different key pairs. In this case it’s not possible to automatically determine which pair to use.

  3. Most probably you’re right. Both @ and ? are valid in an example (depending or what ordering we want to have and which fields are available) but using ? makes more sense.

  4. I didn’t quite get it. What should via. translate to in this case?

Still no more constructive opinions on the @ and ? symbols? I can’t believe noone other than me and fsb find them to be less than ideal.

I don’t have a problem with ‘@:’ and ‘?:’ ;)

Sure one can get used to @ and ? but even now I am accepting these symbols (it requires getting used to it) but rAWTAZ has a point in that it is indeed not intuitive.

What I do like about these symbols are that they are short and quick to write especially in situations where you have many queries to make.

What are the short alternative?

Although a very trivial matter I can actually perceive a painful number of unnecessary forum questions regard this subject.

My main alternative suggestions are simply @self and @foreign (or maybe @other if that’s something people like better). It doesn’t take appreciably more effort to write and it is much clearer. I say this on the same topic as that one should use descriptive variable and function names instead of $r, $l and rvp() when coding, it’s just common sense. It would also leave room for future @whatever when/if needed. See my earlier post for a more elaborate summary :)

I think Seal has a point too. It’s not just about readability; Newcomers have enough to think about as it is, they shouldn’t need to be troubled with memorizing weird symbols that have no intuitive meaning. Better have them learn @self and @other or something, it will make sense to them right from the start.

I couldn’t agree more, @self and @other have my vote. Sure I can get used to the @ and ?, but we are planning to migrate our products to Yii and many of my collegues have to learn Yii. It’s much more readable for a non-Yii programmer.

rAWTAZ

You have a good point actually. I don’t like @other but @self is a good candidate to replace @. @foreign is probably OK for developers familiar with RDBMs.

As long as it it short (less typing), its all good for me.

@self has my vote too

Seal, what do you vote for the other part, @foreign or @other or something else?

Love your looks by the way :mellow:

well


'@foreign'

because it makes the most sense

In attempt to think out of the box, I will throw-in @link,

How about @related ?

@self/@this and @rel (from mdomba’s @related).

I may look like an idiot now, but I haven’t seen a real example use of “?.”. The ?/@foreign/@related/whatever, is it only usable/applicable inside relation definitions (where there is the local and only one foreign table)?

In Qiang’s first post in this thread, he said “?.” can be used “in queries and scopes to represent the table alias prefix to columns” and that it represents “the foreign table”. This makes me wonder how (if at all) it applies in a query/scope where we have more than one foreign/related table. Take the following example:




// Yii 1.1 syntax.

// Find all posts for a public blogs and where the author's account is still active (whatever that means in this example).

Post::model()->with('blog', 'author')->findAll('blog.is_public=true and author.is_active=true');






// Yii 2.0 syntax (attempt).

// Find all posts for a public blogs and where the author's account is still active (whatever that means in this example).

Post::find()->with('blog', 'author')->where('blog.is_public=true and author.is_active=true')->all();



So in the above example, is "?." applicable, and if so what foreign/related table would it reference?