1

I'm building a computer configurator and got four entities.

  1. Customer(not relevant)
  2. Computer(id, customer, serial, date, [components])
  3. ArticleGroup(id, name)
  4. Article(id, name, price, articleGroup, active)

The attribute [components] of the Computer entity should contain an array like this:

    $components = array(
  'CPU' => array(
    'name' => 'Intel Core i7',
    'count' => 1,
    'price' => 275),
  'Mainboard' => array(
    'name' => 'Gigabyte H97-D3H',
    'count' => 1,
    'price' => 75),
  'RAM' => array(
    'name' => '4GB DDRIII Kingston Value Ram',
    'count' => 4,
    'price' => 28),
  'SSD' => array(
    'name' => '512GB Samsung 850 Pro',
    'count' => 1,
    'price' => 400)
  );

The array key price maybe differs from the price of the article entity.(Because of daily prices for hardware or because i think that this customer gets the cpu for less money)

Now the question I don't know how to create this components array collection.

This is my Computer entity:

    <?php

namespace ctcrm\ConfiguratorBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Computer
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Computer
{
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @var string
   * @Assert\NotBlank()
   * @Assert\Length(
   *      min = 12,
   *      max = 12,
   *      minMessage = "no valid serial!",
   *      maxMessage = "no valid serial!"
   * )
   * @ORM\Column(name="serial", type="string", length=12)
   */
  private $serial;

  /**
   * @var \DateTime
   * @Assert\NotBlank()
   * @ORM\Column(name="date", type="date")
   */
  private $date;

  /**
   * @ORM\ManyToOne(targetEntity="Customer")
   * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
   * @Assert\NotBlank()
   **/
  private $customer;

  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * Set serial
   *
   * @param string $serial
   * @return Computer
   */
  public function setSerial($serial)
  {
    $this->serial = $serial;

    return $this;
  }

  /**
   * Get serial
   *
   * @return string
   */
  public function getSerial()
  {
    return $this->serial;
  }

  /**
   * Set date
   *
   * @param \DateTime $date
   * @return Computer
   */
  public function setDate($date)
  {
    $this->date = $date;

    return $this;
  }

  /**
   * Get date
   *
   * @return \DateTime
   */
  public function getDate()
  {
    return $this->date;
  }

  /**
   * @return mixed
   */
  public function getCustomer()
  {
    return $this->customer;
  }

  /**
   * @param mixed $customer
   */
  public function setCustomer($customer)
  {
    $this->customer = $customer;
  }


}

1 Answer 1

4

There is no (reliable) means of having an array in an SQL table. You would need to perform serialization on the data, which is not a recommended practice if you can avoid it.

What you probably want to do is create a Component Entity, with a many-to-many relationship to Computers, using a Join table.

See the Doctrine documentation: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-unidirectional

You'll want the directionality to be from Computers to Components, as a Computer contains a number of Components, but not the inverse. A many-to-one relationship would require duplicate entries for Components (referencing physical objects instead of a classification using a model number), while a many-to-many relationship allows you to populate your components list, and reference them in the computer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.