Return records from a table whose key is referenced in another CakePHP 3

1

I'm using the ORP native CakePHP 3 to retrieve the records of the product table (through the ID) along with the records that reference it with the following method: / p>

public function view($id)
{
    if($this->request->is('get'))
    {
        $product = $this->Products->get($id, [
            'contain' => ['Stores', 'Bookings', 'ProductFeatures', 'ProductMedias']
        ]);
        $this->set('product', $product);

        $bannerType = 2;
        $bannersQuantity = 1;
        $fullBanners = $this->Search->listAllBanners($bannerType, $bannersQuantity);
        $this->set('fullBanners', $fullBanners);

        $logged = $this->Auth->user();
        $this->set('logged', $logged);

        $this->set('pageTitle', $product['product_name'].' - Stores');
    }
}

This is basically returning all the records I need but how do I return each record of the features table referenced in the table product_features

Bank Tables:

CREATE TABLE products (
  id INT AUTO_INCREMENT PRIMARY KEY,
  product_name VARCHAR(255) NOT NULL,
  store_id INT NOT NULL,
  sub_category_id INT NOT NULL,
  quantity INT NOT NULL,
  sold INT NOT NULL,
  description VARCHAR(1000),
  price DECIMAL(7,2) NOT NULL,
  old_price DECIMAL(7,2) NOT NULL,
  visited INT NOT NULL,
  thumbnail VARCHAR(255) NOT NULL,
  status INT NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY store_key (store_id) REFERENCES stores(id),
  FOREIGN KEY sub_category_key (sub_category_id) REFERENCES sub_categories(id)
);

CREATE TABLE sub_categories (
  id INT AUTO_INCREMENT PRIMARY KEY,
  sub_category_name VARCHAR(255) NOT NULL,
  category_id INT NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY category_key (category_id) REFERENCES categories(id)
);

CREATE TABLE features (
  id INT AUTO_INCREMENT PRIMARY KEY,
  feature_name VARCHAR(255) NOT NULL,
  created DATETIME,
  modified DATETIME
);

CREATE TABLE product_features (
  id INT AUTO_INCREMENT PRIMARY KEY,
  feature_value VARCHAR(255) NOT NULL,
  feature_id INT NOT NULL,
  product_id INT NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY feature_key (feature_id) REFERENCES features(id),
  FOREIGN KEY product_key (product_id) REFERENCES products(id)
);

var_dump ($ product) result

object(App\Model\Entity\Product)#249 (25) {
  ["id"]=>
  int(1)
  ["product_name"]=>
  string(6) "Cama X"
  ["store_id"]=>
  int(1)
  ["quantity"]=>
  int(20)
  ["description"]=>
  string(9) "Muito Boa"
  ["price"]=>
  float(550.5)
  ["status"]=>
  int(1)
  ["created"]=>
  object(Cake\I18n\Time)#244 (3) {
    ["time"]=>
    string(24) "2015-07-17T00:00:00+0000"
    ["timezone"]=>
    string(3) "UTC"
    ["fixedNowTime"]=>
    bool(false)
  }
  ["modified"]=>
  object(Cake\I18n\Time)#247 (3) {
    ["time"]=>
    string(24) "2015-07-17T00:00:00+0000"
    ["timezone"]=>
    string(3) "UTC"
    ["fixedNowTime"]=>
    bool(false)
  }
  ["sub_category_id"]=>
  int(8)
  ["sold"]=>
  int(40)
  ["old_price"]=>
  float(400)
  ["visited"]=>
  int(777)
  ["thumbnail"]=>
  string(21) "products/product2.jpg"
  ["product_medias"]=>
  array(2) {
    [0]=>
    object(App\Model\Entity\ProductMedia)#239 (12) {
      ["id"]=>
      int(1)
      ["product_id"]=>
      int(1)
      ["media_id"]=>
      int(1)
      ["created"]=>
      object(Cake\I18n\Time)#232 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["modified"]=>
      object(Cake\I18n\Time)#235 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["[new]"]=>
      bool(false)
      ["[accessible]"]=>
      array(4) {
        ["product_id"]=>
        bool(true)
        ["media_id"]=>
        bool(true)
        ["product"]=>
        bool(true)
        ["media"]=>
        bool(true)
      }
      ["[dirty]"]=>
      array(0) {
      }
      ["[original]"]=>
      array(0) {
      }
      ["[virtual]"]=>
      array(0) {
      }
      ["[errors]"]=>
      array(0) {
      }
      ["[repository]"]=>
      string(13) "ProductMedias"
    }
    [1]=>
    object(App\Model\Entity\ProductMedia)#237 (12) {
      ["id"]=>
      int(8)
      ["product_id"]=>
      int(1)
      ["media_id"]=>
      int(8)
      ["created"]=>
      object(Cake\I18n\Time)#240 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["modified"]=>
      object(Cake\I18n\Time)#231 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["[new]"]=>
      bool(false)
      ["[accessible]"]=>
      array(4) {
        ["product_id"]=>
        bool(true)
        ["media_id"]=>
        bool(true)
        ["product"]=>
        bool(true)
        ["media"]=>
        bool(true)
      }
      ["[dirty]"]=>
      array(0) {
      }
      ["[original]"]=>
      array(0) {
      }
      ["[virtual]"]=>
      array(0) {
      }
      ["[errors]"]=>
      array(0) {
      }
      ["[repository]"]=>
      string(13) "ProductMedias"
    }
  }
  ["product_features"]=>
  array(2) {
    [0]=>
    object(App\Model\Entity\ProductFeature)#218 (13) {
      ["id"]=>
      int(1)
      ["feature_value"]=>
      string(5) "30 Cm"
      ["feature_id"]=>
      int(1)
      ["product_id"]=>
      int(1)
      ["created"]=>
      object(Cake\I18n\Time)#211 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["modified"]=>
      object(Cake\I18n\Time)#214 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["[new]"]=>
      bool(false)
      ["[accessible]"]=>
      array(5) {
        ["feature_value"]=>
        bool(true)
        ["feature_id"]=>
        bool(true)
        ["product_id"]=>
        bool(true)
        ["feature"]=>
        bool(true)
        ["product"]=>
        bool(true)
      }
      ["[dirty]"]=>
      array(0) {
      }
      ["[original]"]=>
      array(0) {
      }
      ["[virtual]"]=>
      array(0) {
      }
      ["[errors]"]=>
      array(0) {
      }
      ["[repository]"]=>
      string(15) "ProductFeatures"
    }
    [1]=>
    object(App\Model\Entity\ProductFeature)#216 (13) {
      ["id"]=>
      int(8)
      ["feature_value"]=>
      string(5) "20 Kg"
      ["feature_id"]=>
      int(2)
      ["product_id"]=>
      int(1)
      ["created"]=>
      object(Cake\I18n\Time)#219 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["modified"]=>
      object(Cake\I18n\Time)#210 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["[new]"]=>
      bool(false)
      ["[accessible]"]=>
      array(5) {
        ["feature_value"]=>
        bool(true)
        ["feature_id"]=>
        bool(true)
        ["product_id"]=>
        bool(true)
        ["feature"]=>
        bool(true)
        ["product"]=>
        bool(true)
      }
      ["[dirty]"]=>
      array(0) {
      }
      ["[original]"]=>
      array(0) {
      }
      ["[virtual]"]=>
      array(0) {
      }
      ["[errors]"]=>
      array(0) {
      }
      ["[repository]"]=>
      string(15) "ProductFeatures"
    }
  }
  ["bookings"]=>
  array(1) {
    [0]=>
    object(App\Model\Entity\Booking)#196 (13) {
      ["id"]=>
      int(1)
      ["product_id"]=>
      int(1)
      ["quantity"]=>
      int(5)
      ["user_id"]=>
      int(1)
      ["created"]=>
      object(Cake\I18n\Time)#194 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["modified"]=>
      object(Cake\I18n\Time)#195 (3) {
        ["time"]=>
        string(24) "2015-07-17T00:00:00+0000"
        ["timezone"]=>
        string(3) "UTC"
        ["fixedNowTime"]=>
        bool(false)
      }
      ["[new]"]=>
      bool(false)
      ["[accessible]"]=>
      array(5) {
        ["product_id"]=>
        bool(true)
        ["quantity"]=>
        bool(true)
        ["user_id"]=>
        bool(true)
        ["product"]=>
        bool(true)
        ["user"]=>
        bool(true)
      }
      ["[dirty]"]=>
      array(0) {
      }
      ["[original]"]=>
      array(0) {
      }
      ["[virtual]"]=>
      array(0) {
      }
      ["[errors]"]=>
      array(0) {
      }
      ["[repository]"]=>
      string(8) "Bookings"
    }
  }
  ["store"]=>
  object(App\Model\Entity\Store)#243 (12) {
    ["id"]=>
    int(1)
    ["store_name"]=>
    string(6) "Loja A"
    ["user_id"]=>
    int(1)
    ["created"]=>
    object(Cake\I18n\Time)#251 (3) {
      ["time"]=>
      string(24) "2015-07-17T00:00:00+0000"
      ["timezone"]=>
      string(3) "UTC"
      ["fixedNowTime"]=>
      bool(false)
    }
    ["modified"]=>
    object(Cake\I18n\Time)#252 (3) {
      ["time"]=>
      string(24) "2015-07-17T00:00:00+0000"
      ["timezone"]=>
      string(3) "UTC"
      ["fixedNowTime"]=>
      bool(false)
    }
    ["[new]"]=>
    bool(false)
    ["[accessible]"]=>
    array(4) {
      ["store_name"]=>
      bool(true)
      ["user_id"]=>
      bool(true)
      ["user"]=>
      bool(true)
      ["products"]=>
      bool(true)
    }
    ["[dirty]"]=>
    array(0) {
    }
    ["[original]"]=>
    array(0) {
    }
    ["[virtual]"]=>
    array(0) {
    }
    ["[errors]"]=>
    array(0) {
    }
    ["[repository]"]=>
    string(6) "Stores"
  }
  ["[new]"]=>
  bool(false)
  ["[accessible]"]=>
  array(10) {
    ["product_name"]=>
    bool(true)
    ["store_id"]=>
    bool(true)
    ["quantity"]=>
    bool(true)
    ["description"]=>
    bool(true)
    ["price"]=>
    bool(true)
    ["status"]=>
    bool(true)
    ["store"]=>
    bool(true)
    ["bookings"]=>
    bool(true)
    ["product_features"]=>
    bool(true)
    ["product_medias"]=>
    bool(true)
  }
  ["[dirty]"]=>
  array(0) {
  }
  ["[original]"]=>
  array(0) {
  }
  ["[virtual]"]=>
  array(0) {
  }
  ["[errors]"]=>
  array(0) {
  }
  ["[repository]"]=>
  string(8) "Products"
}
    
asked by anonymous 06.08.2015 / 15:04

1 answer

1

On your return you have feature_id on your product_features , so for each result of product_features make get()

$this->loadModel('Features')
foreach ($product['product_features'] as $product_feature){;
    $recentFeatures[] = $this->Features->get($product_feature['feature_id']);
}

$product['features'] = $recentFeatures;

loadModel() is to allow you to use a model that was not loaded in this controller in this action, so you do not have to load a Model that will not be used in all the actions of your controller and reduces the response time of the pages     

06.08.2015 / 18:52