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 TActiveRecordHasManyAssociation

Implements the M-N (many to many) relationship via association table. Consider the entity relationship between Articles and Categories via the association table Article_Category.

+---------+            +------------------+            +----------+
| Article | * -----> * | Article_Category | * <----- * | Category |
+---------+            +------------------+            +----------+

Where one article may have 0 or more categories and each category may have 0 or more articles. We may model Article-Category object relationship as active record as follows.

class ArticleRecord
{
    const TABLE='Article';
    public $article_id;

public $Categories=array(); //foreign object collection.

    public static $RELATIONS = array
    (
        'Categories' => array(self::MANY_TO_MANY, 'CategoryRecord', 'Article_Category')
    );

    public static function finder($className=__CLASS__)
    {
        return parent::finder($className);
    }
}
class CategoryRecord
{
    const TABLE='Category';
    public $category_id;

    public $Articles=array();

    public static $RELATIONS = array
    (
        'Articles' => array(self::MANY_TO_MANY, 'ArticleRecord', 'Article_Category')
    );

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

The static $RELATIONS property of ArticleRecord defines that the property $Categories has many CategoryRecords. Similar, the static $RELATIONS property of CategoryRecord defines many ArticleRecords.

The articles with categories list may be fetched as follows.

$articles = TeamRecord::finder()->withCategories()->findAll();

The method with_xxx() (where xxx is the relationship property name, in this case, Categories) fetchs the corresponding CategoryRecords using a second query (not by using a join). The with_xxx() accepts the same arguments as other finder methods of TActiveRecord.

TActiveRecordRelation
Extended by TActiveRecordHasManyAssociation
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/TActiveRecordHasManyAssociation.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 using association table.

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

Parameters

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

Returns

array
2 arrays of source keys and foreign keys from the association table.
protected TDbTableInfo
# getAssociationTable( )

Returns

TDbTableInfo
association table information.
protected TDbTableInfo
# getSourceTable( )

Returns

TDbTableInfo
source table information.
protected TDbTableInfo
# getForeignTable( )

Returns

TDbTableInfo
foreign table information.
protected TDataGatewayCommand
# getCommandBuilder( )

Returns

TDataGatewayCommand
protected TDataGatewayCommand
# getForeignCommandBuilder( )

Returns

TDataGatewayCommand
protected
# fetchForeignObjects( array & $results, array $foreignKeys, mixed $indexValues, mixed $sourceKeys )

Fetches the foreign objects using TActiveRecord::findAllByIndex()

Fetches the foreign objects using TActiveRecord::findAllByIndex()

Parameters

$results
array
field names
$foreignKeys
array
foreign key index values.
$indexValues
$sourceKeys
protected TActiveRecord
# createFkObject( string $type, array $row, array $foreignKeys )

Parameters

$type
string
active record class name.
$row
array
row data
$foreignKeys
array
foreign key column names

Returns

TActiveRecord
public
# createCommand( TSqlCriteria $criteria, TTableInfo $foreignKeys, array $indexValues, array $sourceKeys )

Parameters

$criteria
TSqlCriteria
$foreignKeys
TTableInfo
association table info
$indexValues
array
field names
$sourceKeys
array
field values
protected string
# getSourceColumns( array $sourceKeys )

Parameters

$sourceKeys
array
source table column names.

Returns

string
comma separated source column names.
protected string
# getAssociationJoin( array $foreignKeys, array $indexValues, array $sourceKeys )

SQL inner join for M-N relationship via association table.

SQL inner join for M-N relationship via association table.

Parameters

$foreignKeys
array
foreign table column key names.
$indexValues
array
source table index values.
$sourceKeys
array
source table column names.

Returns

string
inner join condition for M-N relationship via association table.
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 .
protected TDbCommandBuilder
# getAssociationTableCommandBuilder( )

Returns

TDbCommandBuilder
Methods inherited from TActiveRecordRelation
__call(), __construct(), fetchResultsInto(), findForeignKeys(), findForeignObjects(), getContext(), getCriteria(), getIndexValues(), getObjectHash(), getSourceRecord(), populateResult(), setObjectProperty(), 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