0

I have a table "test_brand" and a table "test_vehicles". In "test_vehicles" I put a parameter which is "brand_id". Now I want to get in my template a collection with all brand and for each brand all their vehicles.

I found no tutorials about this on the net so I tried myself but I can't found the solution, can you help me please ?

I have the model for brand and vehicles. So I made for each a simple Collection like :

<?php
namespace Test\Vehicles\Model\ResourceModel\Brand;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection {

  protected $_idFieldName = \Test\Vehicles\Model\Brand::BRAND_ID;

  /**
  * Define resource model
  *
  * @return void
  */
  protected function _construct() {
    $this->_init('Test\Vehicles\Model\Brand', 'Test\Vehicles\Model\ResourceModel\Brand');
  }

  public function getBrandShowcase($brand, $vehicle) {
    $this->addFieldToSelect('*')
    ->join(
      array('vehicle' => $this->_vehicle->getResource()->getMainTable()),
      'main_table.'.$this->_brand->getIdFieldName().' = vehicle.brand_id',
      array('vehicle_name' => 'name')
    );
    return $this;
  }

}

And in my block I call the function but I get an error :

Exception #0 (Exception): Item (Test\Vehicles\Model\Brand) with the same ID "1" already exists.

Here is my model :

<?php
namespace Test\Vehicles\Model;

use \Magento\Framework\Model\AbstractModel;

class Brand extends AbstractModel {
  const BRAND_ID = 'entity_id'; // We define the id fieldname

  /**
  * Prefix of model events names
  *
  * @var string
  */
  protected $_eventPrefix = 'test'; // parent value is 'core_abstract'

  /**
  * Name of the event object
  *
  * @var string
  */
  protected $_eventObject = 'brand'; // parent value is 'object'

  /**
  * Name of object id field
  *
  * @var string
  */
  protected $_idFieldName = self::BRAND_ID; // parent value is 'id'

  /**
  * Initialize resource model
  *
  * @return void
  */
  protected function _construct() {
    $this->_init('Test\Vehicles\Model\ResourceModel\Brand');
  }
}

And there is the ressource model :

<?php
namespace Test\Vehicles\Model\ResourceModel;

use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Brand extends AbstractDb {

  /**
  * Initialize resource model
  *
  * @return void
  */
  protected function _construct() {
    // Table Name and Primary Key column
    $this->_init('test_brand', 'entity_id');
  }
}

I thinked about a solution and maybe I need to use the collection of vehicle to get all vehicles about a brand_id but I don't know how to build it. Can you help me please ?

  • Maybe I've found a solution... I'm working in – Kpone Dec 29 '16 at 11:28
0

I found the solution. In my Brand model I put a function to get the Collection of Vehicles and in my template in my foreach when I get my Brand object I call this method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.