Sylius: [BUG] Unable to create user programmatically ("customer_id has no default value")

Created on 8 Jun 2016  Â·  4Comments  Â·  Source: Sylius/Sylius

When I try to create a user (both the customer-entity first and then the user-entity), i get an Exception from MySQL, stating that customer_id has no default value (allthough the customer was assigned with a valid id).

What I did:

        use Sylius\Component\User\Model\Customer;
        use Sylius\Component\User\Model\User;
        .....
        $repositoryUser = $this->container->get('sylius.repository.user');
        $repositoryCustomer = $this->container->get('sylius.repository.customer');

        $customer = new Customer();

        $customer->setFirstName('John');
        $customer->setLastName('Doe');
        $customer->setEmail(time().'[email protected]');

        $repositoryCustomer->add($customer);

        $user = new User();

        $user->setCustomer($customer);
        $user->setPassword('test');

        // gives valid ID of customer
        var_dump($user->getCustomer()->getId());

        // makes bumm:
        $repositoryUser->add($user);

This happens then:

An exception occurred while executing 'INSERT INTO sylius_user (username, username_canonical, enabled, salt, password, last_login, confirmation_token, password_requested_at, locked, expires_at, credentials_expire_at, roles, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, 0, "i62gagr3qvwww8g0kcs8swcwkgg0goo", "test", null, null, null, 0, null, null, "a:1:{i:0;s:9:\"ROLE_USER\";}", "2016-06-08 16:10:43", null]:

SQLSTATE[HY000]: General error: 1364 Field 'customer_id' doesn't have a default value

It's notable that the INSERT INTO-Statement does not add the customer_id to the INSERT INTO statement.

By the way, It's might also be notable that the Documentation mentions here and there a method "createNew" on the repositories, which isn't available any more.

Help Wanted

Most helpful comment

5181 #5207 and here comes the 3rd one :dancer: !

https://github.com/Sylius/Sylius/blob/3e7fd7a4f801eeaa5f3f359c3b9ec44205fe0c4f/src/Sylius/Bundle/FixturesBundle/DataFixtures/ORM/LoadUsersData.php

Docs will be quite outdated till 1.0 so your best bet is to dive into the codebase.
For cases like yours(and for those likened above) its best to peek into FixturesBundle.
It has everything you need.

All 4 comments

5181 #5207 and here comes the 3rd one :dancer: !

https://github.com/Sylius/Sylius/blob/3e7fd7a4f801eeaa5f3f359c3b9ec44205fe0c4f/src/Sylius/Bundle/FixturesBundle/DataFixtures/ORM/LoadUsersData.php

Docs will be quite outdated till 1.0 so your best bet is to dive into the codebase.
For cases like yours(and for those likened above) its best to peek into FixturesBundle.
It has everything you need.

Thanx for the hint! CreateUserCommand.php is also a good template:
vendor/sylius/sylius/src/Sylius/Bundle/UserBundle/Command/CreateUserCommand.php

@pjedrzejewski i assume this can be closed aswell as #5181 and #5207

@okwinza Thanks for providing support man, this is a huge help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

reyostallenberg picture reyostallenberg  Â·  3Comments

hmonglee picture hmonglee  Â·  3Comments

eb22fbb4 picture eb22fbb4  Â·  3Comments

inssein picture inssein  Â·  3Comments

crbelaus picture crbelaus  Â·  3Comments