Strona główna
  lazy eager

kaniagostyn *UKS Kania Gostyń

JSON for related models. Right now CJSON can't include related models in it's JSON output.


I saw in another thread that getIterator and offsetExists need to be overrided.

I guess it would be a lot better if CJSON was smart enough (or had options) to include related models. I think it should include related models that were eager loaded (not lazy loaded) by default.

For Ajax applications JSON format is usually the main output method and returning results including joined table columns is very often required, so I think JSON output with related models is pretty important.

method getRelated. I have Post model relations

return array( 'author'=>array(self::BELONGS_TO, 'Author', 'authorId'), 'commentCount'=>array( self::STAT, 'Comment', 'postId' ), 'comments'=>array( self::HAS_MANY, 'Comment', 'postId', 'limit'=>'10', 'with'=> array('author','repliesCount'), ), );

and Comment model relation

return array( 'author'=>array( self::BELONGS_TO, 'Author', 'id' ), 'repliesCount'=>array( self::STAT, 'Comment', 'repliedToPostId' ) );


When i'm getting data like this :

$comments = $post->getRelated('comments',false,array('with'=>array('author','repliesCount')));


comment author loaded using eager loading and repliesCount by lazy loading

when getting data like this

$comments = Comment::model()->with('repliesCount','author')->findAll();

both loaded by eager loading


what the difference ?

thanks


How do I check if a model has related models without triggering lazy loading. Hi all,


I am wondering how I would go about checking for the presence of related models after an eager load.

It seems to me like if I check for : count($modelA->modelB) for instance

This will trigger a lazy load and defeats my purpose wihich is to check if the eager load has retrieved models.

don't know if this is obvious or if I am clear

any help appreciated

Thanks

Thomas

Extend model with new attributes and populate by join query. Hi Tommy,

the one with the relation I already had. Problem is that if you do it like that, it seems to me that you have to implement sorting on the column by yourself. And using eager or lazy loading - it will be a mess.


In your view
echo $userRecord->group->your-attribute;



What I want to do is this
echo $userRecord->group-table-attribute;

I can add the attribute to the User model, but how can I tell the model to populate it with a specific field from the data if the specific field shows up in the data?

I.e., make the attribute available through the model, if the attribute is contained in the dataset.

Perhaps there is a way to access the underlying raw query result and implement an onload event that copies it to the attribute if present.

In standard sql thinking, for this kind of problem I would create a virtual table by using a view and issue a query on this one. So the data would be put together right away. In Yii, it seems to me that I require a model for each view, and may lead to a model flood :)

Cheers,
Hein

Help with AR relationships.
Doesn't this return all Paragraphs with their related ChangeRequests? I want just the ChangeRequests that are related to an Article (via Paragraphs).

I haven't tried exactly this, but you should be able to declare a condition like this
... ->with(some-relationship=>array('condition'=>'some-condition'), ...)-> ...

BTW this is eager loading. Alternatively, for lazy loading you should be able to specify a condition and 'with' in a relationship declaration.

Edit:
See also with() in class reference.

/Tommy

Sharding.
How to deal with relational queries?

This database scaling screencast (apart from rails-specific tips) contains introductory information about sharding map techniques.

AR models should know which related data is in the same database, and in those cases one can use eager loading. Otherwise the newly introduced anti-together lazy loading option should be used.

Edit:

When multiple connections are used, how do we merge the data together when calling findAll()?

If sharding takes effect, it is developers' task to ensure that unaccessible tables are not involved (i.e. determining foreign keys before findAll).
This post has been edited by pestaa: 24 August 2009 - 11:14 AM
ActiveRecord has_many through and scoping. Thank you for your nice questions.

1. named_space: We don't have this concept. The implementation is pretty straightforward, however, since Yii has CDbCriteria, which represents everything needed by a query. And one may set up a mapping between scope names and and criterias. We may consider building this into the core in the near future.

2. has_many through: While Yii supports many-to-many association, it doesn't support this for the moment. I think it's a good idea that we support this in future.

3. The query conditions need to be specified in relations(). For on-the-fly query condition like you described, Yii only supports it in an eager approach currently:

<?php
$user=User::model()->with(array(
    'posts'=>array(
        'condition'=>'something=:something',
        'params'=>array(':something'=>10),
    )
))->find(1);


We may support it in future for lazy approach.

Usage of scopes in relations().
What is the generated SQL? Note that 'with' option is only used by lazy loading.

Ah ok, how can I handle it  for the following?

Vehicle::model()->with('arrTeaserImage')->findAll();

Has it got to be like that:

Vehicle::model()->with('arrTeaserImage.objDisplayType:teaser')->findAll();

?

EDIT:

Yes, that works, but how can I define that in function "relations", for eager loading?


Lazy and eager loading... Trying to get the best mix.. Hi all

I've met one little problem:

<?php $record = Record::model()->findByPk($id);
The code above will fetch all record attributes, but what if I don't need them all?

Actually I need the single attribute and I have no need to get all the attributes from database. It decreases performance as you know.

Can anyone help me with this point? I'm sure that a solution exists, but it is not clear to me... ???

named scopes for related models in eager loading problem. Hi,

I am trying to use named scope in a 'with' call
I apply scopes to the related model.

I think this might be a bug but let's see

the problem :

if no records are returned for the 'filtered' related model, trying to access those (absent records) will trigger a lazy loading on the related model whereas it should not, since we want to be able to account for the fact that there are no records within the named scope.

ex :

2 models

Artist
Event

a scope for Event

active (condition : active = 1)

2 events for an Artist (id = 2)
those events are inactive

the eager call :

$a = Artist::model()->with('Event:active')->findByPk(2);

a count on $a->Event should return 0 since there are no active events for Artist 2

but it returns 2

because a lazy load has been performed without scope so all events have been retrieved

setting one of the event as active allows the correct behavior as a count on $a->Event returns 1

I might be missing something

but it seemed to me like this was not an expected behavior

thanks




How to use column aliases in AR ?. Thank you, it works.

but ....

This code works ...

$criteria = new CDbCriteria(array('select' => 'CONCAT_WS(" - ", column1, column2) as description'));
echo table1::model()->find($criteria)->description;


But this doesn't:
in table1 relations

'relation' => array (self::BELONGS_TO, 'table2', 'fk', 'select' => 'CONCAT_WS(" - " ,??.column1, ??.column2) as description'),

and both lines:

echo table1::model()->find()->relation->description;
echo table1::model()->with('relation')->find()->relation->description;

Throws [tt]CDbException - Active record "table2" is trying to select an invalid column "CONCAT_WS(" - "". Note, the column must exist in the table or be an exp​ression with alias.[/tt]

I was trying it with mysql database.

Am I doing something wrong or there is no possibility to select that expresion neither with lazy nor eager loading?

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • shirli.pev.pl


  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • losegirl.htw.pl
  •  Menu
     : kaniagostyn *UKS Kania Gostyń
     : Łazy Dom Wczasowy Posejdon
     : Łazy morze pokoje do wynajęcia
     : Lazy Town Bing Bang
     : Łazy gmina Jerzmanowice-Przeginia
     : Łazy hodowla psów rasowych
     : Lazy Hours Vol 1 rapidshare
     : Łazy nad morzem transport
     : Lazy Hours Vol1
     : Łazy Dom Kultury
     : Łazy ŚRODKI Wczasowe
     . : : .
    Copyright (c) 2008 kaniagostyn *UKS Kania Gostyń | Designed by Elegant WPT