#[ORM\OneToOne(targetEntity: Element::class)]
#[JoinColumn(name: 'element', referencedColumnName: 'id')]
private Element $element;
referencedColumnName: 'id'
у Product
должен совпадать с name: 'id'
у Element
. У вас будто бы не так from
принимает на вход имя класса (e.g. 'App/Entity/Result'
или Result::class
), а не таблицыchecking_index
добжно быть r.checking_index
EntityManager
, а не создавать их самим.UserRepository
прописать в маппинге сущности@ORM\Entity(repositoryClass="UserRepository")
$ids = [1, 2, 3, 4];
$this->getEntityManager()->getConnection()
->executeStatement('UPDATE some_table SET some_field = ? WHERE id IN (?)', [
'value',
$ids,
], [
1 => Connection::PARAM_INT_ARRAY,
]);
$builder = $this->getEntityManager()->getConnection()->createQueryBuilder();
$builder
->update('some_table')
->set('some_field', ':value')
->where($builder->expr()->in('id', ':ids'))
->setParameter('value', 'value')
->setParameter('ids', $ids, Connection::PARAM_INT_ARRAY)
;
$builder->execute()
$this->getEntityManager()
->createQuery('UPDATE App\Entity\SomeEntity se SET se.someValue = :value WHERE se.id IN (:ids)')
->execute(new ArrayCollection([
new Parameter('value', 'value'),
new Parameter('ids', $ids),
]))
);
$builder = $this->createQueryBuilder('se');
$builder
->update()
->set('se.someValue', ':value')
->where($builder->expr()->in('uu.id', ':ids'))
->setParameter('value', 'value')
->setParameter('ids', $ids)
;
$builder->getQuery()->execute();
maker:
root_namespace: 'App\Common'
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
AppСommon:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Common/Entity'
prefix: 'App\Common\Entity'
alias: AppСommon
use Pagerfanta\Pagerfanta;
class PaginatedCollection
{
private int $page;
private int $size;
private int $total;
private int $pages;
private array $items;
public function __construct(Pagerfanta $pagerfanta)
{
$this->page = $pagerfanta->getCurrentPage();
$this->size = $pagerfanta->getMaxPerPage();
$this->total = $pagerfanta->getNbResults();
$this->pages = $pagerfanta->getNbPages();
$this->items = iterator_to_array($pagerfanta);
}
public function getPage(): int
{
return $this->page;
}
public function getSize(): int
{
return $this->size;
}
public function getTotal(): int
{
return $this->total;
}
public function getPages(): int
{
return $this->pages;
}
public function getItems(): array
{
return $this->items;
}
}
// Repository
public function findPaginatedBySupplierId(int $supplier_id)
{
return new Pagerfanta(DoctrineORMAdapter($this->getSupplierIdQueryBuilder($supplier_id)));
}
private function getSupplierIdQueryBuilder(int $supplier_id)
{
$qb = $this->createQueryBuilder('sj');
$qb = $qb
->select("sj")
->orderBy('sj.datetime', 'ASC')
->andWhere("sj.supplier = :supplier_id")
->setParameter("supplier_id", $supplier_id);
return $qb;
}
// Controller
public function action()
{
// ...
return $this->json($this->paginatedCollection($repository->findPaginatedBySupplierId($id), $request));
}
// AbstractController
protected function paginatedCollection(Pagerfanta $pagination, Request $request, int $size = 20): PaginatedCollection
{
$pagination = $this->paginate($pagination, $request, $size);
return new PaginatedCollection($pagination);
}
protected function paginate(Pagerfanta $pagination, Request $request, int $size = 20): Pagerfanta
{
$pagination->setMaxPerPage($size);
$pagination->setCurrentPage($request->query->getInt('page', 1));
return $pagination;
}
{
"page": 1,
"size": 20,
"total": 76,
"pages": 4,
"items": [...]
}
// Этот класс в бандле
/** @MappedSuperclass */
class BaseAnimal
{
/** @Column(type="string") */
protected $name;
/** @Column(type="integer") */
protected $size;
/** @Column(type="boolean") */
protected $canFly;
}
// Этот класс в проекте
/** @Entity */
class Animal extends BaseAnimal
{
/** @Column(type="string") */
private $id;
/** @ManyToOne(targetEntity="Person") */
private $owner;
}
referencedColumnName: Name of the primary key identifier that is used for joining of this relation.
/**
* @OneToMany(targetEntity="Address", mappedBy="customer", cascade={"persist", "remove"})
* @JoinColumn(name="customer_id", referencedColumnName="customer_id")
*/
protected $addresses;
Address::$customer_id
- это primary key?/**
* @Entity
* @Table(name="`address`")
*/
class Address
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="IDENTITY")
*/
protected $address_id;
/**
* @Column(type="string")
*/
protected $firstname;
/**
* @ManyToOne(targetEntity="Customer", inversedBy="address")
* @JoinColumn(name="customer_id", referencedColumnName="customer_id")
*/
protected $customer;
}
/**
* @Entity(repositoryClass="\Core\Repositories\CustomerRepository")
* @Table(name="`customer`")
*/
class Customer
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="IDENTITY")
*/
protected $customer_id;
/**
* @OneToMany(targetEntity="Address", mappedBy="customer", cascade={"persist", "remove"})
*/
protected $addresses;
public function __construct()
{
$this->addresses = new ArrayCollection();
}
}
referencedColumnName: Name of the primary key identifier that is used for joining of this relation.
/**
* @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", mappedBy="ttn_boxberry", cascade={"persist", "remove"})
* @JoinColumn(name="order_code", referencedColumnName="id")
*/
protected $boxberry_ttn;
/**
* @Entity
* @Table(name="`ttn_boxberry`")
*/
class BoxberryTtn
{
/**
* @Id
* @Column(type="string")
* @GeneratedValue(strategy="NONE")
*/
protected $id;
public function __construct(string $id)
{
$this->id = $tid;
}
}
order_code
/**
* @Entity
* @Table(name="`order`")
*/
class Order
{
/**
* @Id
* @Column(type="integer")
*/
protected $order_code;
/**
* @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", mappedBy="ttn_boxberry", cascade={"persist", "remove"})
* @JoinColumn(name="order_code", referencedColumnName="order_code")
*/
protected $boxberry_ttn;
}
/**
* @Entity
* @Table(name="`order`")
*/
class Order
{
/**
* @Id
* @Column(type="integer")
*/
protected $order_id;
/**
* @Id
* @Column(type="integer")
*/
protected $order_code;
/**
* @Column(type="integer")
*/
protected $warehouse_id;
/**
* @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", cascade={"persist", "remove"})
* @JoinColumn(referencedColumnName="order_code")
*/
protected $boxberry_ttn;
public function __construct(int $order_id, string $order_code)
{
$this->order_id = $order_id;
$this->order_code = $order_code;
}
}
/**
* @Entity
* @Table(name="`ttn_boxberry`")
*/
class BoxberryTtn
{
/**
* @Id
* @Column(type="string")
* @GeneratedValue(strategy="NONE")
*/
protected $order_code;
/**
* @Column(type="string")
*/
protected $ttn_num;
/**
* @Column(type="float", scale=2)
*/
protected $delivery_cost;
public function __construct(Order $order)
{
$this->order_code = $order->getOrderCode();
}
}
class AccountRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Account::class);
}
}
class AccountRepository extends \Doctrine\ORM\EntityRepository
{
}
App\Repository\AccountRepository:
arguments:
- 'App\Entity\Account' # Entity class
factory: ['@doctrine', 'getRepository']
// $env = ...;
$kernel = new AppKernel($env, false);
$kernel->loadClassCache();
Один и тот же скрипт использует несколько клиентов, какая именно база данных используется - определяется через субдомен. Список субдоменов и их баз данных хранится в служебной базе данных.
/**
* @ORM\OneToOne(targetEntity="App\Post" , fetch="EAGER")
*/
$query = $em->createQuery("SELECT u FROM App\User u");
$query->setFetchMode("App\User", "posts", \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER);
$query->execute();