I
I
IvanN7772018-05-16 10:28:11
Doctrine ORM
IvanN777, 2018-05-16 10:28:11

I work with the 1 to 1 relationship doctrine, I select the related entity, but for some reason it is empty?

productmodel

Asu\Models\ProductModel:
  type: entity
  table: product
  id:
    id:
      type: integer
      length: 10
      column: product_id
      generator:
        strategy: AUTO
  fields:
    priceId:
        type: integer
        length: 10
        column: price_id
  oneToOne:
    price:
      targetEntity: Asu\Models\PriceModel
      joinColumn:
        name: price_id
        referencedColumnName: price_id

PriceModel
Asu\Models\PriceModel:
  type: entity
  table: price 
  id:
    id:
      type: integer
      length: 10
      column: price_id
      generator:
        strategy: AUTO

The PHP code
has a protected price; property and set a getter and a setter for it.
public function setPrice($price)
    {
        $this->price = $price;
        return $this;
    } 
    public function addPrice($price)
    {
        $this->setPrice($price);
    }
    public function getPrice()
    {
        return $this->price;
    }

If entities are used without links, then they work, but the result is not returned by the link, null.
$product = $productRepo->findOneBy(['code' => 'АН', 'number' => '101', 
            'color' => 'БД']);
        $price = $product->getPrice();

The code doesn't crash, it finds the product, but when I try to display the price, it returns a simple Null. Although there is a record in the database.
How to get related record by oneToOne

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
voronkovich, 2018-05-16
@voronkovich

Here is the missing piece:

fields:
    priceId:
        type: integer
        length: 10
        column: price_id

The doctrine itself will create all the necessary fields for communication.

I
IvanN777, 2018-05-17
@IvanN777

The problem turned out to be that I used __call () it was just on the links and it broke.
Though normally gave out values ​​at normal sampling.

public function __call($namefunction, $arguments)
    {
        if (strlen($nameFunction) < 4):
            return;
        endif;
        $prefix = substr($nameFunction, 0, 3);
        $name = lcfirst(substr($nameFunction, 3, strlen($nameFunction) - 3));
        $inFields =  array_search($name, self::$fields);
        if ($prefix == 'get'):
            if ($inFields):
                return $this->$name;
            endif;
        endif;
        if ($prefix == 'set'):
            if ($inFields):
                $value = $arguments[0];
                $this->$name = $value;
            endif;
        endif;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question