1

Estou utilizando o ORM nativo do CakePHP 3 para recuperar os registros da tabela produto (através do ID) juntamente com os registros que o referencia com o seguinte método:

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');
    }
}

Esta sendo retornado basicamente todos os registros que necessito porém como faço para retornar cada registro da tabela features referenciado na tabela product_features

Tabelas do banco:

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)
);

Resultado do var_dump($product)

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"
}
9
  • @ErlonCharles este $this->Products->get() vei por padrão com o a geração do arquivo pela bake
    – Ricardo
    Commented 6/08/2015 às 13:28
  • Estava procurando o recursive na documentação do 3, como ficaria o find sem ele então?
    – Ricardo
    Commented 6/08/2015 às 13:59
  • Posta o resultado do seu $this->Products->get() pra eu analisar, porque de acordo com a documentação era pra retornar seus product_features também. mas se quiser pode tentar esse find $this->Products->find('first', [ 'conditions' => ['Products.id' => $id], 'contain' => ['Stores', 'Bookings', 'ProductFeatures', 'ProductMedias'] ]); Commented 6/08/2015 às 14:04
  • $this->Products->get() da erro de falta de parametro
    – Ricardo
    Commented 6/08/2015 às 14:07
  • 1
    @ErlonCharles não funcionou a segunda abordagem, tive até de modificar pois o 'first' não é mais suportado, tive de fazer um ->first()
    – Ricardo
    Commented 6/08/2015 às 14:16

1 Resposta 1

1

No seu retorno você possui feature_id na sua product_features, portanto para cada resultado da product_features faça um get()

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

$product['features'] = $recentFeatures;

loadModel() serve para te permitir usar um model que não foi carregado neste controller nessa action especifica, assim você não precisa carregar um Model que não será utilizado em todas as actions do seu controller e reduz o tempo de resposta das páginas

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .