php - Doctrine - ManytoMany Filter Result -
i need filter result many many relationship in doctrine.
class users extends recorditem { /** * @id @column(type="integer") @generatedvalue * @var int **/ protected $id; /** * @manytomany(targetentity="company") * @jointable(name="users_join_company", * joincolumns={@joincolumn(name="user_id", referencedcolumnname="id")}, * inversejoincolumns={@joincolumn(name="company_id", referencedcolumnname="id")} * ) */ protected $companys; /** * @column(type="string", length=100) * @var string */ protected $username; //edit - > added array collection - forgotten public function __construct() { $this->companys = new arraycollection(); } } class company extends recorditem { /** * @id @column(type="integer") @generatedvalue * @var int **/ protected $id; /** * @column(type="string", length=100) * @var string */ protected $company_name; }
so far i'm able query company following code, there proper way add filter? example: there 3 company in array collection, want return 1 specify company "id"
$user = $entitymanager->getrepository('users')->findoneby(['id'=>1]); $companys = $user->companys; // hope return company id 1 foreach($companys $company){ echo $company->company_name; }
if want filter collection, use arraycollection ...
example :
// guess companies arraycollection $mydesiredcompanies = $companies->filter(function (company $entry) { return ($entry->getid() == 1); });
it filter collection , return new collection desired results
Comments
Post a Comment