Показаны сообщения с ярлыком doctrine. Показать все сообщения
Показаны сообщения с ярлыком doctrine. Показать все сообщения

вторник, 1 февраля 2022 г.

[Doctrine] how to replace Many-To-Many to three entities

When you need to put new field/database columns into Many-To-Many, relationship, you will need to switch to intermediate entity. In my case it is StudentKlass.

Imagine three entities: Team - StudentTeam - Student

use Doctrine\ORM\Mapping as ORM;

class Team {
/**
* @ORM\OneToMany(targetEntity=StudentTeam::class, mappedBy="team", cascade={"persist"})
*/
private $studentTeams;

public function __construct() {
$this->studentTeams = new ArrayCollection();
}
...
}

class StudentTeam {
/**
* @ORM\ManyToOne(targetEntity=Student::class, inversedBy="teams")
* @ORM\JoinColumn(name="student_id", referencedColumnName="id")
*/
private $student;

/**
* @ORM\ManyToOne(targetEntity=Team::class, inversedBy="studentTeams")
*/
private $team;
...
}

class Student {
/**
* @ORM\OneToMany(targetEntity=StudentTeam::class, mappedBy="student")
*/
private $teams;
private $name;
private $email;
...

} 

Also don't forget to create getters, adders & removers!

You can use example how to use it in Symfony forms, which has example how to use nested forms:

class TeamType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('studentTeams', CollectionType::class, [
'entry_type' => TeamStudentType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
]);
...
}

class TeamStudentType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add(
$builder
->create('student', FormType::class, [
'data_class' => Student::class,
])
->add('email', TextType::class)
->add('name', TextType::class)
)
;
}
}
}

четверг, 22 августа 2019 г.

Doctrine persists extra entities at update/merge fix

If you have batch create and update action simultaneously (persisting entity if it doesn't exists in DB or update it otherwise), made like in this documentation, and you noticed that it creates redundant entities, it means Doctrine think they are new. Enable "detach" cascade (transitional) operation in Doctrine like this:

/**
* @OneToMany(targetEntity="Customer", mappedBy="product", cascade={"detach"})
*/
protected $customers;

If you are not in dev environment, do not forget to run orm:clear-cache:metadata command.

четверг, 25 апреля 2019 г.

[Sylius] fix mysql errors with INSERT INTO sylius_shipping_method_translation after services.yaml locale changing

If you have errors similar to INSERT INTO sylius_shipping_method_translation (name, description, locale, translatable_id) VALUES (?, ?, ?, ?)' with params [null, null, "lv", 1] error after services.yaml changing locale , also with another Sylius tables, try to manually add these records in database.

For example, if you have problems with product db table, go to admin/products/{id}/edit and add translation to your main locale, do it for every product.

It occurs when you switch the main locale, then adding something to cart (in another words create an order). Order entity has relation to Shipping method, which has relation to Shipping method translation entity, which doesn't exists with needed locale. So Doctrine thinks it should persist before creating an Order, so there occurs an error.

воскресенье, 10 февраля 2019 г.

How to fix "phpcr/phpcr-implementation no matching package found"

If you have error:

Using version ^1.4 for doctrine/phpcr-odm
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "4.2.*"
Your requirements could not be resolved to an installable set of packages.

 Problem 1
   - doctrine/phpcr-odm 1.4.4 requires phpcr/phpcr-implementation ^2.1 -> no matching package found.
   - doctrine/phpcr-odm 1.4.3 requires phpcr/phpcr-implementation ^2.1 -> no matching package found.
   - doctrine/phpcr-odm 1.4.2 requires phpcr/phpcr-implementation ^2.1.0 -> no matching package found.
   - doctrine/phpcr-odm 1.4.1 requires phpcr/phpcr-implementation ^2.1.0 -> no matching package found.
   - doctrine/phpcr-odm 1.4.0 requires phpcr/phpcr-implementation ^2.1.0 -> no matching package found.
   - Installation request for doctrine/phpcr-odm ^1.4 -> satisfiable by doctrine/phpcr-odm[1.4.0, 1.4.1, 1.4.2, 1.4.
3, 1.4.4].

Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
  see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
- It's a private package and you forgot to add a custom repository to find it

Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.


You should add your project's composer.json these lines:
"provide": {
    "phpcr/phpcr-implementation": "2.1.0"},

Symfony 4.2.3.

четверг, 1 ноября 2018 г.

[sylius standalone bundles] "DataSource::__construct() must be an instance of Doctrine\ORM\QueryBuilder, null given" error fix

It means that your Grid repository method doesn't return QueryBuilder, maybe at now you return array/one entity objects with getResult() method inside Repository method.

Example of correct QueryBuilder repository method:

public function createPrivateListQueryBuilder(int $residentId): QueryBuilder{    return $this->createQueryBuilder('a')        ->leftJoin('a.user', 'user')        ->where('user.id = :id')        ->setParameter('id', $residentId);}

Notice that there is no getResult()/getOneOrNullResult()/getSingleResult().
Appears in versions Sylius 1.0 - 1.3, maybe earlier.

среда, 4 июля 2018 г.

Notice: undefined index id after changing MongoDB id to string

after using a php annotation @Mongo\Id(strategy="NONE") you can receive Notice: undefined index id PHP error, for this just run symfony cache:clear command.

четверг, 28 сентября 2017 г.

Sylius - The table with name 'sylius_order' already exists

1) be sure you have added your class' model parameter to config.yml, as described in Sylius documentation;

2) be sure you do not use old parent class somewhere else in config.yml - for example, this will cause error below. Do not forget to change it as new class:
sylius_resource:
    resources:
        sylius.accountant:
            classes:
                model: AppBundle\Entity\Order
        sylius.worker:
            classes:
                model: AppBundle\Entity\Order


суббота, 29 июля 2017 г.

Symfony how to debug DQL

use command doctrine:query:dql
For example, php bin/console doctrine:query:dql "SELECT a FROM AppBundle:Attachment a"

четверг, 18 августа 2016 г.

symfony fixtures cannot delete or update a parent row

* you can add to problem entity field, which is described in error onDelete="set null"

* or deny to purge your database, add to your command key --append.

вторник, 21 июня 2016 г.

Doctrine: String data, right truncated: 1406 Data too long for column 'column_name' at row 2

It is only one of the way. If you want to convert field to another type, you can fix it with changing values length in the database, according to your changes length.