Status update: ActiveRecord

It is not trivial at all.

@self and @relative is going to be used a lot, so it should as clear and self-descriptive as possible, even if it means that we nitpick.

It can maybe be seen as trivial when you look at it in isolation; just remember that a framework is essentially a collection of trivial things.

Which makes them non-trivial. ;)

Oh no …

In the example of page one we have the table

tbl_order_item

> order_id (FK1, PK)

> item_id (FK2, PK)

> quantity

> subtotal

> id

Has the new framework an approach implemented to access the "meta-informations" in the join table (namely: quantity, subtotal and id)?

Coksnuss

Yes.

is this correct?

@related will work in queries and scopes ONLY when the model has exactly one relation?

Guess it is only valid in conditions we define for a specific relation to enable yii to choose a different alias name if they are ambigous. This was not possible in 1.1

I guess so too. But what are these conditions?

Given the code he presented in that original post, can anyone devise examples of the use of ?. (or @related.) in queries and scopes?

Maybe I missed this, but will the new ActiveRecord support saving related models? And if so, what will the syntax be?

(samdark said this is a good thing about Eloquent in a different thread, but I thought this would be the better place to ask. :) )

yJeroen

Most probably yes. There will be dirty attributes support so it will be relatively straightforward to save related records. Currently there’s no support for it and I’m not sure if it will be in github alpha release.

I vote for

@own and @rel

Both just 4 chars. I think shortcuts for table name/alias was one of reasons why this was introduced, so keep it short. It’s also readable. e.g.:




'on' => '@own.type = "product" AND @rel.amount > 0'



On another topic, regarding the eager loading:

Now there are some not obvious (at least from the beginning) rules to automatically detect whether relation should be eagerly or lazy loaded:

  • one-to-one relation will be eagerly loaded

  • one-to-many / many-many relation will be lazy loaded if ‘limit’ is set (query paginated) or eagerly loaded otherwise

  • if we set ‘together’ option then one-to-many / many-many relation will be eagerly loaded (regardless of ‘limit’)

Will this be changed somehow in a new active record version?

In 1.1 I modified the generator so when creating a many-many instead of an if/else check depending on if it was a relation table, I just had it create both and modified the template to add a "//pivot table" comment next to the tables that were relational.

That way I get both many_many tables along with pivot table relations in case of meta data.

It would be nice to have access to both in 2.0.

edit:

Also, occasionally there’s a many_many relation where the pivot table would have

> id (PK)

> order_id (FK1, PK)

> item_id (FK2, PK)

And the relation is just through ‘order’ and ‘item’, but there’s a id as well. Would be nice if while checking if there is a many_many relation it could skip id if there were 3 pk’s. But that’s probably a fringe case.

I just learned scopes:( now I will have to re learn it haha. doh .

Looks like overall good improvements. With fewer find() methods it will be alot easier to learn yii imho, I remember it beeing very confusing "which one should I use??? waahh"

A question,


foreach(Customer::find() as  $customer

Does this mean $customers = Customer::find() works to? or is $Customers::find()->all() a must?

$model->save(array(‘related’=>array(‘relationName’=>array($relatedModels)));

Would this be nice? would be easy atleast to build the array and then just write $model->save($array);

And it would support several relations and several records per relation.

Just an idea

I dont use other languages XD so for me @self sounds the most clear, after all “@me” sounds more like it’s referring to the developer…

and not to forget, we use self in yii 1.1.X today:p would make the transition smoother and it’s still very clear what it’s refer to.

I vote for @self

about @rel and @related it’s, a big HMMM. I agree with rawtaz I generally doent like shortened versions but in this case it feels like it is very clear what it refers to for most people.

I vote for @related but save me the possibility to change my mind :P

Hi, I’ve writen my idea about active record’s design. Here it is: http://www.yiiframework.com/forum/index.php/topic/35018-an-idea-for-joiningless-activerecord/

Could you guys take a look and then give out your opinion?

If I look at the following two pages, is the relations() method scrapped? Or just not implemented yet?

https://github.com/yiisoft/yii2/blob/master/docs/api/db/ActiveRecord.md

This new scheme of things also raises some questions:

  • If I load a relation using $model->relationName. the magic __get is used to load the relation getRelationName method into _related. However… What if I want to reload the data?

  • Isn’t a single place to define relations more straight forward? And then use __get and __set to manipulate your related data.

Note that if relations() is scrapped for the idea of seperate methods for each relationship and scope… I’m thinking of always using two different behaviors for each of my models. One for relations and one for scopes. This way, it’s more categorized imho.

Relations now are to be implemented differently: https://github.com/yiisoft/yii2/blob/master/docs/api/db/ActiveRecord.md

If I may ask, what were the pro’s and con’s to implement it like this?

With this implementation, if I have a model with eg. 5 relations, I need to create a method for each of them. They’re also not grouped and since they look the same as other getXxx methods for custom attributes, in a list of all the methods in an IDE it can get unorganized.

It’s more explicit, better IDE support, one can add relations on the fly and via behaviors.