I need some help writing a ORM based retrieval of objects

So I have a table Organizations - and another additional tables Organizations_addresses. I have managed to create the "reverse join" on organization->id to organizations_addresses->org_id column.. So when I retrieve organizations I can get all the addresses associated with the organizations.
Now my organization_addresses table has a column called is_main which takes values 0 or 1.

Now when retrieving organization objects, I want to annotate the main site address..
something like this- >
Organization::objects->annotate(array(
'mainsite'=> addresses->filter(array('is_main' == 1))));

-but I can't find documentation on how to write the filter on "addresses" which are fields for Organization
my Organization model is like this->

$meta = array(
'table' => ORGANIZATION_TABLE,
'pk' => array('id'),
'joins' => array(
'users' => array(
'reverse' => 'User.org'
),
'cdata' => array(
'constraint' => array('id' => 'CompanyCdata.org_id')
),
'entries' => array(
'constraint' => array(
'id' => 'DynamicFormEntry.object_id',
"'O'" => 'DynamicFormEntry.object_type',
),
'list' => true,
),
'addresses' => array(
'reverse' => 'Companyaddress.cos',
)
),

where Companyaddress is a class

I find the ORM in osticket to be really powerful and interesting.. I would like to use this as much without having to write SQL queries.

Thanks for all the help.

So I realized after delving into the source-

i can access the addresses field with "".. so essentially what i am checking is "addressesis_mainsite => 1"..

'has_hq' => SqlAggregate::COUNT(SqlCase::N()->when(array('addresses__is_main' => 2),1)->otherwise(null)),

returns has_hq = 1 when is_main is 2.

Now, I need to return the "addresses" object where is_main = 2.. how can i write that as an annotate clause

Should i look more into Q? or SqlExpr ? Or is it something else to return the object in a where clause?

That's encouraging ?

I managed to get the data by doing this ->
Organization::objects()
->select_related('addresses')
->filter(array("addresses__is_main" => 1));

Write a Reply...