Question

enter image description here

here is my containable condition

$userStatusCondition = array('AND' => array(
                                                     'OR'=> array(
                                                                    'TransactionStatus.buyer_status_id'=>$user_status_id,
                                                                    'TransactionStatus.seller_status_id'=>$user_status_id,
                                                                )
                                                            )
                                                );

Merge conditions (other conditions + user_status condition)

$transactions = $this->Transaction->find('all',array(
                                                            'conditions'=> Set::merge($otherConditions, $userStatusCondition),
                                                            'recursive'=>2));

in TransactionModel I set the

public $actsAs = array('Containable');

public $belongsTo = array(
             'UserStatus' => array(
        'className' => 'UserStatus',
        'foreignKey' => 'buyer_status_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
            'UserStatus' => array(
        'className' => 'UserStatus',
        'foreignKey' => 'seller_status_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
   );

problems

1) I don't like to use recursive 2: it decreases the find method - how ever I think after containable correctly I think I need to remove recursive
2) I have four user_statuses in User_Status table 1)waiting 2)attention 3)block 4)Complete which I want to find transactions filtered by buyer_status_id or seller_status_id

I donno how to create new behavior - there is small article about it Using Contain

Was it helpful?

Solution

Another way of linking to table relationship is to use bindModel to force Join.

Below is a code that binds 4 tables Items->Categories->Sections->divisions. You can find anything within the range of the relationship.

$this->unbindModel(array(
    'belongsTo' => array('Category')
));

$this->bindModel(array(
    'hasOne' => array(
        'Category' => array(
            'foreignKey' => false,
            'conditions' => array('Category.id = Item.category_id')
        ),
        'Section' => array(
            'foreignKey' => false,
            'conditions' => array('Section.id = Category.section_id')
        )
        'Division' => array(
            'foreignKey' => false,
            'conditions' => array('Division.id = Section.division_id')
        )
    )
));
$result = $this->find('first', array(
    'conditions' => array('Item.id' => $id),
    'contain' => array('Category', 'Section', 'Division'),
    'fields' => array('Division.id')
));

By doing this you can freely query anything in the range of the tables.

More Info here: http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top