Relations in Model Tables

Discuss everything related to ApPHP MVC Framework. All versions. Any questions related to the Framework developing or using should be posted to this forum.

Moderators: ne_moj, alexmst, zewa

Relations in Model Tables

Postby paragate on Dec 13th, '16, 07:35

hi,
i'm a little confused about RELATIONS when working with Model. Maybe you can explain functionality
of CActiveRecord.530.php private function _getRelations().

the reason is that i have the following working code

Code: Select all
SELECT
upc_ingredient_mappings.*,
upc_ingredients.*,
upc_recipes.*
FROM
upc_ingredient_mappings
LEFT JOIN upc_recipes
ON upc_ingredient_mappings.recipe_id = upc_recipes.id
LEFT JOIN upc_ingredients
ON upc_ingredient_mappings.ingredient_id = upc_ingredients.id
WHERE
upc_recipes.id = '1'

the if statement
Code: Select all
if(
    $relationType == self::HAS_ONE ||
    $relationType == self::BELONGS_TO ||
    $relationType == self::HAS_MANY ||
    $relationType == self::MANY_MANY
   )
   { //code...}


which works fine as a many to many relaion in mysql but i do not know how to implement it into the model

$relationType refer to the function _relations() as in line 1171 $rel = $this->_relations();

That function _relations()
Code: Select all
/**
     * Used to define relations between different tables in database and current $_table
    * This method should be overridden
    */
   protected function _relations()
   {
      return array();
   }

should be overwritten and is done in the model php scripts. What confused me is the relations
and how the work?

in Yii framework it is done as example with the tables status and users:
// User model relations
1. 'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
// User belongs_to a status, because it is a child table.

// Status model relations
2. 'users' => array(self::HAS_MANY, 'User', 'status_id'),
// Status has_many users, because it is a parent table.
User avatar
paragate
Master
Master
 
Posts: 139
Joined: Nov 13th, '14, 13:04
Location: Denmark, Odense

Re: Relations in Model Tables

Postby ne_moj on Dec 13th, '16, 10:16

Code: Select all
class Listings extends CActiveRecord
{
    // Prefix take equal 'pfx_'
    private $_table = 'listings';
    private $_tableTranslation = 'listing_translations';
    private $_tableListingsCategories = 'listings_categories';

    // .... code ....

    /**
     * Defines relations between different tables in database and current $_table
     * @return array
     */
    protected function _relations()
    {
        return array(
            // `pfx_listings`.id - Field over which the join
            'id' => array(
                self::HAS_MANY, // Relation type
                $this->_tableTranslation, // Name table ('listign_translations')
                'listing_id', // `pfx_listing_translations`.lsting_id
                'condition'=>"`language_code` = '".A::app()->getLanguage()."'", // additional join options
                'joinType'=>self::LEFT_OUTER_JOIN,
                'fields'=>array('business_name' => 'name', 'business_description' => 'name') // Name chosen fields
            ),
            0 = array(
                self::HAS_MANY,
                $this->_tableListingsCategories,
                'listing_id', // `pfx_listings_categories`.lsting_id
                // In this case, I can not use id as the key, because it is already in use, so the field is set via the parameter 'parent_key'
                'parent_key' => 'id', // `pfx_listings`.id; Field over which the join
                'condition' => '',
                'joinType' => self::INNER_JOIN,
                'fields' => array('category_id')
            )
       );

       /* Listings::model->findAll() It makes such a request:
       'SELECT `pfx_listings`.*, `pfx_listing_translations`.business_name as name, `pfx_listing_translations`.business_description as description, `pfx_listings_categories`.category_id LEFT OUTER JOIN `pfx_listing_translations` ON `pfx_listings`.id = `pfx_listing_translations`.listing_id AND `language_code` = 'en' INNER JOIN `pfx_listings_categories` ON `pfx_listings`.id = `pfx_listings_categories`.listing_id'
       */
    }
}
User avatar
ne_moj
Master
Master
 
Posts: 208
Joined: Jan 1st, '16, 14:11

Re: Relations in Model Tables

Postby paragate on Dec 13th, '16, 20:01

Thanks, thinking of some kind of this, can recognize the use of Parent_id,
Was also playing a little around with Datagrid 8.1 my intention was the Diagram attached as to do a recipe database CRUD,
but it seems there are no easy way other than simply start mvc programming.....
Attachments
ER_Diagram_recipe.jpg
ER_Diagram_recipe.jpg (206.77 KiB) Viewed 701 times
User avatar
paragate
Master
Master
 
Posts: 139
Joined: Nov 13th, '14, 13:04
Location: Denmark, Odense

Re: Relations in Model Tables

Postby ne_moj on Dec 14th, '16, 08:38

Hi

I almost did not work with the DataGrid, because I can not imagine how it is implemented there. However, I see no problem to do it in the MVC Framework.
User avatar
ne_moj
Master
Master
 
Posts: 208
Joined: Jan 1st, '16, 14:11


Return to ApPHP MVC Framework {developers/users}

Who is online

Users browsing this forum: No registered users and 1 guest