JOIN on 3 tables with Propel ORM
One of my client is going to release a new product, the developer who was doing the coding left the project (with completing 85%).
A custom SQL query needs to be executed using Propel ORM. The query is as follows:
$sql = "SELECT
`domain`.*,
`domain_registrar`.`name` AS `registrar_name`,
`domain_account`.`id` AS `account_id`,
`domain_account`.`username` AS `account_username`,
DATEDIFF(`domain`.`expire_date`, NOW()) AS `days_to_expiration`
FROM
`domain`
LEFT JOIN `domain_account` ON `domain_account`.`id`=`domain`.`domain_account_id`
LEFT JOIN `domain_registrar` ON `domain_registrar`.`id`=`domain_account`.`domain_registrar_id`
ORDER BY
`days_to_expiration`,
`domain_registrar`.`name`,
`domain_account`.`username`,
`domain`.`domain`";
I have very basic knowledge of propel ORM, so I thought any of the experts can help me to solve it. The db scheme is like below:
<table name="domains" phpName="Domains">
<column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" />
<column name="account_id" type="INTEGER" required="true" />
<column name="domain" type="VARCHAR" size="255" required="true" />
<column name="expiration" type="DATE" />
<foreign-key foreignTable="domain_accounts" phpName="DomainAccounts" refPhpName="Domains">
<reference local="account_id" foreign="id" />
</foreign-key>
</table>
<table name="domain_accounts" phpName="DomainAccounts">
<column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" />
<column name="registrar_id" type="INTEGER" required="true" />
<column name="username" type="VARCHAR" size="255" required="true" />
<column name="password" type="VARCHAR" size="255" required="true" />
<foreign-key foreignTable="domain_registrars" phpName="DomainRegistrars" refPhpName="DomainAccounts">
<reference local="registrar_id" foreign="id" />
</foreign-key>
</table&g开发者_运维技巧t;
<table name="domain_registrars" phpName="DomainRegistrars">
<column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" />
<column name="value" type="VARCHAR" size="255" required="true" />
</table>
In Propel 1.5 it would become something like this (untested, from the top of my head):
DomainsQuery::create()
->joinWith( 'Domains.DomainAccounts' )
->joinWith( 'Domains.DomainRegistrars' )
->withColumn( 'DATEDIFF(`domain`.`expire_date`, NOW())', 'DaysToExpiration' );
精彩评论