使用Propel ORM加入3张桌子

时间:2011-01-31 15:46:53

标签: php orm propel

我的一位客户将发布一款新产品,正在进行编码的开发人员离开了该项目(完成了85%)。

需要使用Propel ORM执行自定义SQL查询。查询如下:

$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`";

我对推进ORM有非常基本的了解,所以我认为任何专家都可以帮助我解决它。 db方案如下所示:

<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>
  <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>

1 个答案:

答案 0 :(得分:3)

在Propel 1.5中它会变成这样的东西(未经测试,从头顶开始):

DomainsQuery::create()
    ->joinWith( 'Domains.DomainAccounts' )
    ->joinWith( 'Domains.DomainRegistrars' )
    ->withColumn( 'DATEDIFF(`domain`.`expire_date`, NOW())', 'DaysToExpiration' );