garyconstable
5/31/2015 - 11:54 PM

Save a craft CMS Relationship

Save a craft CMS Relationship

<?php

namespace Craft;

class TestVariable
{
    public function p($data = array() ){
        echo '<pre>'.print_r($data, true).'</pre>';
    }
    
    public function d($data = array() ){
        echo '<pre>'.print_r($data, true).'</pre>';
        die();
    }
    
    
    public function listIds(){
        
        $this->p(' --- BEGIN HERE --- ');   
        
        //this returns an element criteria model
        $o_criteria = craft()->elements->getCriteria(ElementType::Category);
        
        //set the model params - level 1 order by level limit 20!
        $o_criteria->group  = 'catParent2';
        $o_criteria->order  = 'level';
        $o_criteria->level  = 1;
        $o_criteria->limit  = 20;
        
        //check we have some matching categories
        if( $o_criteria->getDescendants()->total() > 0 ){
            
            //loop through 
            foreach ($o_criteria as $o_entry)
            {
                $this->p("Level 1: " . $o_entry->title);
                
                //if we have some children, i.e the next level down
                if( $o_entry->getDescendants()->total() > 0 ){
                    
                    //get the current levels children
                    $o_children = $o_entry->getChildren();
                    
                    //loop each child
                    foreach ($o_children as $d){

                        $this->p("&nbsp;&nbsp; Level 2: " . $d->title);
                    }

                }

            }
            
        }
        
        $this->p(' --- END HERE --- ');   
    }
    
    /**
     * get the matrix block id's using the hardcoded preference block handle;
     * @return type
     */
    public function getPreferenceIds(){
        
        $o_user_model = craft()->userSession->getUser();
        
        $a_id = array();
        $this->preferencesBlocks = array(
            'preference'
        );
        
        foreach( $this->preferencesBlocks as $x){
            $q  = 
            " select ".$o_user_model->id." as ownerId, fields.id as fieldId , types.id as typeId from craft_fields fields LEFT JOIN craft_matrixblocktypes types ON fields.id = types.fieldId 
            where lower(fields.type) = 'matrix' and lower(fields.handle) like '%%".$x."%%' 
            -- AND types.handle = 'contact' ";
            
            $o_db_command = craft()->db->createCommand($q)->queryAll(true);
            $a_id[ $x ] = $o_db_command;
        }
        return $a_id;
    }
    
    
    
    public function test(){
        
        //user model
        $o_user_model = craft()->userSession->getUser();
        
        
        //list id's in the system so we can make some tests (these will bet the cats that we select)!
        $this->listIds();
        
        
        //get the id's for the matrix block
        $matrix_block_ids = $this->getPreferenceIds();
        
        
        //The Craft\FieldModel of the field which has the associations
        $o_field = craft()->fields->getFieldByHandle('preference');
        
        
        //The source, in this case the matrix block that has the field
        //when we know the id we can write $o_source = craft()->matrix->getBlockById(27); , when we dont knwo the id we can write
        $o_source = new MatrixBlockModel();
        $o_source->fieldId    = intval($matrix_block_ids['preference'][0]['fieldId']); 
        $o_source->ownerId    = intval($matrix_block_ids['preference'][0]['ownerId']); 
        $o_source->typeId     = intval($matrix_block_ids['preference'][0]['typeId']); 
        
        
        if( craft()->matrix->saveBlock( $o_source ) ) {

        }
        else {
  
            $a_errors = $o_source->getErrors();
            
        }

        
        //3. An array of element id's you wish to relate to the field
        //$a_relations = $o_user_model->preference->ids();
        //we can see these using the call above $this->listIds();
        $a_relations = array(
            14
        );
        
        
        //4. save the relations
        craft()->relations->saveRelations( $o_field , $o_source, $a_relations);
        
        
        $this->d(' --- DIED HERE --- ');
        
        
    }
    

}