1
我有一個AutoComplete在下面的代碼中使用模型名稱。我很難將其擴展爲使自動填充包含品牌名稱。例如,如果有ST1和ST2型號的索尼品牌,我希望能夠鍵入索尼並獲得兩行 - 索尼ST1和索尼ST2,但是如果我鍵入ST1,我想要一個索尼排 - 索尼ST1。使用Symfony和Doctrine擴展FilteringSelect/AutoComplete
控制器代碼
/**
* @View()
*/
public function getModelsAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
$modelName = str_replace('*', '%', $request->get('name'));
$em = $this->getDoctrine()->getManager();
$queryBuilder = $em->createQueryBuilder()
->select('m')
->from('AppBundle:Model', 'm');
$queryBuilder->where(
$queryBuilder->expr()->like('m.name', '?1')
);
$queryBuilder->setParameter(1, $modelName);
$query = $queryBuilder->getQuery();
$modelCollection = $query->getResult();
$data = [];
foreach($modelCollection as $m)
{
$item = [
'id' => $m->getId(),
'name' => $m->getName(),
];
$data[] = $item;
}
return $data;
}
品牌和型號(註釋)
/**
* @var ArrayCollection $brands
* @ORM\ManyToMany(targetEntity="Model", cascade={"persist"})
* @ORM\JoinTable(name="brand_model",
* joinColumns={@ORM\JoinColumn(name="brand_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="model_id", referencedColumnName="id", unique=true, nullable=false)}
* )
*/
protected $models = null;
數據庫之間的連接
品牌
dev=# \d brand
Table "public.brand"
Column | Type | Modifiers
------------+--------------------------------+-------------------------------------------
id | integer | not null
name | character varying(64) | default NULL::character varying
active | boolean | not null
deleted_at | timestamp(0) without time zone | default NULL::timestamp without time zone
comment | character varying(64) | default NULL::character varying
Indexes:
"brand_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "manufacturer_brand" CONSTRAINT "fk_170c6d4844f5d008" FOREIGN KEY (brand_id) REFERENCES brand(id)
TABLE "brand_model" CONSTRAINT "fk_8c6cbbce44f5d008" FOREIGN KEY (brand_id) REFERENCES brand(id) ON DELETE CASCADE
模式
dev=# \d model
Table "public.model"
Column | Type | Modifiers
------------+--------------------------------+-------------------------------------------
id | integer | not null
name | character varying(64) | default NULL::character varying
comment | character varying(64) | default NULL::character varying
active | boolean | not null
deleted_at | timestamp(0) without time zone | default NULL::timestamp without time zone
Indexes:
"model_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "brand_model" CONSTRAINT "fk_8c6cbbce7975b7e7" FOREIGN KEY (model_id) REFERENCES model(id)
brand_model
dev=# \d brand_model
Table "public.brand_model"
Column | Type | Modifiers
----------+---------+-----------
brand_id | integer | not null
model_id | integer | not null
Indexes:
"brand_model_pkey" PRIMARY KEY, btree (brand_id, model_id)
"uniq_8c6cbbce7975b7e7" UNIQUE, btree (model_id)
"idx_8c6cbbce44f5d008" btree (brand_id)
Foreign-key constraints:
"fk_8c6cbbce44f5d008" FOREIGN KEY (brand_id) REFERENCES brand(id) ON DELETE CASCADE
"fk_8c6cbbce7975b7e7" FOREIGN KEY (model_id) REFERENCES model(id)