1

I tried to look up on Google but didn't find anyone with such a problem. I think I did everything like the documentation guides but I guess I'm missing something

So I have a form with checkbox like this:

    $builder->add(
        'productTypes',
        EntityType::class,
        array(
            'label'        => 'Available for products',
            'class'        => 'ShopBundle:ProductType',
            'choice_label' => 'name',
            'multiple'     => true,
            'expanded'     => true,
            'by_reference' => false,
        )
    );

When I'm editing everything goes smooth, I can edit existing entry and check or uncheck this checkbox, it saves properly, but when I try to add new Object I get error:

PHP Fatal error: Call to a member function add() on null in C:\xampp\htdocs\uniacar-sf\src\ShopBundle\Entity\ProductAttribute.php on line 188

This is my controller action:

public function editAction(Request $request, $id = null)
{
    $this->setMenuTab('cars', 'admin');
    $productTypes = new ArrayCollection();

    if (!empty($id)) {
        $attribute = $this->getRepo(ProductAttribute::class)->find($id);
        $this->setTitle('admin.cars.attributes.edit');

        foreach ($attribute->getProductTypes() as $value) {
            $productTypes->add($value);
        }
    } else {
        $attribute = new ProductAttribute();
        $this->setTitle('admin.cars.attributes.new');
    }

    $form = $this->createForm(ProductAttributeForm::class, $attribute);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $attribute = $form->getData();

        foreach ($productTypes as $productType) {
            if (false === $attribute->getProductTypes()->contains($productType)) {
                $productType->getAttributes()->removeElement($attribute);
                $this->db()->persist($productType);
            }
        }

        $this->db()->persist($attribute);
        $this->db()->flush();

        return $this->redirectToRoute('carAdmin', array('tab' => 'attributes'));
    }

    $this->setVariables(
        array(
            'form'      => $form->createView(),
            'attribute' => $attribute,
        )
    );

    return $this->response();
}

$this->db() is my shortcut for $this->getDoctrine()->getManager()

And this is definition part of ProductAttribute that relates to ProductType:

/**
 * Constructor
 */
public function __construct() {
    $this->productTypes = new ArrayCollection();
}

/**
 * Many Attributes have Many ProductTypes
 * @ORM\ManyToMany(targetEntity="ProductType", mappedBy="attributes", cascade={"persist"})
 */
private $productTypes;

/**
 * @param ProductType $productType
 */
public function addProductType(ProductType $productType)
{
    $this->productTypes->add($productType);
    $productType->addProductAttribute($this);
}

/**
 * @param ProductType $productType
 */
public function removeProductType(ProductType $productType)
{
    $this->productTypes->removeElement($productType);
}

Also there is part of ProductType Entity that relates to ProductAttribute:

/**
 * Constructor
 */
public function __construct() {
    $this->attributes = new ArrayCollection();
}

/**
 * Many ProductTypes have Many Attributes
 * @ORM\ManyToMany(targetEntity="ProductAttribute", inversedBy="productTypes")
 * @ORM\JoinTable(name="product_type_to_attribute")
 */
private $attributes;


/**
 * @param ProductAttribute $attribute
 */
public function addProductAttribute(ProductAttribute $attribute)
{
    if (!$this->attributes->contains($attribute)) {
        $this->attributes->add($attribute);
    }
}

public function removeProductAttribute(ProductAttribute $attribute)
{
    $this->attributes->removeElement($attribute);
}

I tried to follow Symfony Embed Form Tutorial (How to Embed a Collection of Forms) I know that in this case there is no embeded collection (I have another field in this Entity, that is embeded collection of forms and it works just fine) but from what I understand relations are the same in this case, it's many to many so I have to tell the Symfony how to treat relations, add and remove objects. I dumped data that comes in POST but it's the same as for edition - productType is there. Any ideas why do I get this error?

It fires in ProductAttribute Entity in the line $this->productTypes->add($productType);

EDIT: I updated the controller code, I messed up the logic about unlinking ProductType from ProductAttribute. But it doesn't have any impact on the problem, still the same 500 error when I try to save new object.

EDIT2: I can't get stack trace from Symfony because I get ordinary browser 500 error (probably because it's Fatal Error, I found it in apache logs). The line in controller which creates error is $form->handleRequest($request);.

11
  • Could you tell us, on which line in controller it breaks? On handle request, on flush, on in some other place? Commented Nov 18, 2017 at 14:01
  • It breakes in Entity, in method addProductType in line I mentioned at the end of post Commented Nov 18, 2017 at 18:34
  • I understand that it breaks in entity. But it goes into entity because of some line in controller. Could you tell us which line it is in controller? Or attach full stack trace. Commented Nov 18, 2017 at 21:01
  • I can't get stack trace, despite app being run in dev mode with debug set to true, I get browser typical 500 error, I found the line causing this error in apache logs only. This kind of error never happened to me before, I guess it's because it's Fatal Error that Symfony can't dump me the stack trace.. Commented Nov 18, 2017 at 21:53
  • Where is addProductType() being called? Commented Nov 18, 2017 at 22:05

2 Answers 2

1

This is not a Collection of Forms, but you are using collection specific method, this is not a good practice, however, you don't need this below code when you create a new object.

foreach ($productTypes as $value) {
    if (false === $attribute->getProductTypes()->contains($value)) {
            $attribute->getProductTypes()->removeElement($value);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I need it, without it I can't unlink (I mean when I uncheck the checkbox and save, it still remains connected). Oh, you mean when creating the object... When I'm creating the object the $productTypes array is empty, so nothing happens in this part of the code.
Also I just noticed, that suddenly even using this I can't unlink ProductType from ProductAttribute.. Yesterday exactly the same code have been working differently.. Now I don't know anything anymore :(
Ok, sorry for spam. You pointed me in the right direction, I messed up the logic and tried to remove element only in case this element is not in the collection (sic!), now the controller code is fixed with the right logic :)
0

So, I haven't found solution to the problem but I solved it somehow by fixing file structure of my project (moved bundle's Resources from general Resources folder to Bundle's Resources folder). I have no idea why this fixed the issue and what is even the connection between working but not proper folder structure and submitting forms but now it works, so I will mark the question as answered.

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.