PRADO Component Framework for PHP 5
  • Home
  • About
  • Testimonials
  • Demos
  • Download
  • Documentation
  • Forum
  • Development
  • Tutorials
  • Class Docs
  • API Manual
  • Wiki

Packages

  • None
  • System
    • Caching
    • Collections
    • Data
      • ActiveRecord
        • Relations
        • Scaffold
          • InputBuilder
      • Commom
        • Sqlite
      • Common
        • Mssql
        • Mysql
        • Oracle
        • Pgsql
        • Sqlite
      • DataGateway
      • SqlMap
        • Configuration
        • Statements
    • Exceptions
    • I18N
    • IO
    • Security
    • Util
    • Web
      • Javascripts
      • Services
      • UI
        • ActiveControls
        • WebControls
    • Xml
  • Wsat
    • pages
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo

Class TActiveRecordHasOne

TActiveRecordHasOne models the object relationship that a record (the source object) property is an instance of foreign record object having a foreign key related to the source object. The HAS_ONE relation is very similar to the HAS_MANY relationship (in fact, it is equivalent in the entities relationship point of view).

The difference of HAS_ONE from HAS_MANY is that the foreign object is singular. That is, HAS_MANY will return a collection of records while HAS_ONE returns the corresponding record.

Consider the entity relationship between a Car and a Engine.

+-----+            +--------+
| Car | 1 <----- 1 | Engine |
+-----+            +--------+

Where each engine belongs to only one car, that is, the Engine entity has a foreign key to the Car's primary key. We may model Engine-Car object relationship as active record as follows.

class CarRecord extends TActiveRecord
{
    const TABLE='car';
    public $car_id; //primary key
    public $colour;

    public $engine; //engine foreign object

    public static $RELATIONS=array
    (
        'engine' => array(self::HAS_ONE, 'EngineRecord')
    );

   public static function finder($className=__CLASS__)
   {
           return parent::finder($className);
   }
}
class EngineRecord extends TActiveRecord
{
    const TABLE='engine';
    public $engine_id;
    public $capacity;
    public $car_id; //foreign key to cars

   public static function finder($className=__CLASS__)
   {
           return parent::finder($className);
   }
}

The static $RELATIONS property of CarRecord defines that the property $engine that will reference an EngineRecord instance.

The car record with engine property list may be fetched as follows.

$cars = CarRecord::finder()->with_engine()->findAll();

The method with_xxx() (where xxx is the relationship property name, in this case, engine) fetchs the corresponding EngineRecords using a second query (not by using a join). The with_xxx() accepts the same arguments as other finder methods of TActiveRecord, e.g. with_engine('capacity < ?', 3.8).

TActiveRecordRelation
Extended by TActiveRecordHasOne
Package: System\Data\ActiveRecord\Relations
Copyright: Copyright © 2005-2014 PradoSoft
License: http://www.pradosoft.com/license/
Author: Wei Zhuo <weizho[at]gmail[dot]com>
Version: $Id$
Since: 3.1
Located at Data/ActiveRecord/Relations/TActiveRecordHasOne.php
Methods summary
protected
# collectForeignObjects( array & $results )

Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.

Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.

Parameters

$results
array
original results.
public array
# getRelationForeignKeys( )

Returns

array
foreign key field names as key and object properties as value.

Since

3.1.2
protected
# setObjectProperty( TActiveRecord $source, array $properties, array & $collections )

Sets the foreign objects to the given property on the source object.

Sets the foreign objects to the given property on the source object.

Parameters

$source
TActiveRecord
source object.
$properties
array
foreign objects.
$collections
array
foreign objects.

Overrides

TActiveRecordRelation::setObjectProperty()
public boolean
# updateAssociatedRecords( )

Updates the associated foreign objects.

Updates the associated foreign objects.

Returns

boolean
true if all update are success (including if no update was required), false otherwise .
Methods inherited from TActiveRecordRelation
__call(), __construct(), fetchResultsInto(), findForeignKeys(), findForeignObjects(), getContext(), getCriteria(), getIndexValues(), getObjectHash(), getSourceRecord(), populateResult(), setResultCollection()
Terms of Service | Contact Us
PRADO v3.2.4 API Manual API documentation generated by ApiGen 2.8.0
Copyright © 2006-2014 by the PRADO Group.
Powered by PRADO