ManyToMany relationships with FactoryMuffin in php? - codeception

I'm using Codeception and I have an entity Courier related by a ManyToMany relations to users.
I'm using the have method from DataFactory like this
$courier = $I->have(Courier::class);
$courier->addOwner($user);
But I would like to do it like this
$courier = $I->have(Courier::class, ['owners' => [$user]]);
Is this possible?

I found this solution
$courier = $I->have(Courier::class, ["addThisOwners" => [$user1]]);
$factory->_define(Courier::class, [
"subject" => Faker::word(1, 5),
])->setCallback(function ($obj) {
foreach ($obj->addThisOwners as $owner) {
$obj->addOwner($owner);
}
});

Related

How to update multiple models at once by an array that include post_id

I have a Model Post and there is an array that includes post_id like this:
hashes = [[post_id: 1, info: 'foo'],
[post_id: 3, info: 'bar'],
[post_id: 4, info: 'foobar']]
I can update models by this code.
hashes.each do |h|
post = Post.find(h[:post_id])
next if post.nil?
post.update(info: h[:info])
end
But the array size is normally over 3000, if possible I want to avoid call update so many times.
How can I implement this function without using each loop?
It sounds like you are looking for Update records - from the documentation Updating multiple records; different col with different data
You can do like this Example:
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)

Symfony Equal to and Not Equal to Criteria on Entity

i want to perform a Find operation on Entity Results.
$stores = c2gr($this, 'EducateToolsBundle:Stores')->findBy(['selectOption' => true, 'center_type_id' => 9])
i want to do center_type_id != 9
Just use a DQL or QueryBuilder.
$repository
->createQueryBuilder('s')
->where('s.selectOption = :selectOption')
->andWhere('s.center_type_id <> :center_type_id')
->setParameters([
'selectOption' => true
'center_type_id' => 9,
])
->getQuery()
->getResult();
You should be able to achieve that by using Criteria class. Something like this should work:
use Doctrine\Common\Collections\Criteria;
$criteria = Criteria::create();
$criteria->where($criteria->expr()->neq('center_type_id', 9));
$criteria->andWhere($criteria->expr()->eq('selectOption', true));
$entityRepository = $this->getDoctrine()->getRepository('C2EducateToolsBundle:Stores');
$result = $entityRepository->matching($criteria);
NOTE: above will work if you are in class which extends Symfony\Bundle\FrameworkBundle\Controller\Controller since it is using getDoctrine method. If you are not in such class you should either inject EntityManager or get it from service container.
For more details you should check this answer

Yii2 - custom order in the models get relationship

What is the correct way to make the custom order in the models get relationship? Is it possible to do this? This is my current code:
public function getBoardPosts()
{
return $this->hasMany(BoardPosts::className(), ['topic_id' => 'id'])->orderBy('order ASC');
}
Yes it is. Here is an example from the guide:
class Customer extends ActiveRecord
{
public function getBigOrders($threshold = 100)
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])
->where('subtotal > :threshold', [':threshold' => $threshold])
->orderBy('id');
}
}
Please note that you may have to quote the field (or better yet name your columns using non-SQL words):
return $this->hasMany(BoardPosts::className(), ['topic_id' => 'id'])->orderBy('`order` ASC');

NHibernate Fetch when querying for single object

I'm trying to optimize an NHibernate query:
var profile = dc.Profiles.FirstOrDefault(p => p.IdProfile == idProfile);
I would like it to load a collection of Rights. I did this:
var profile = dc.Profiles.Fetch(x => x.Rights).FirstOrDefault(p => p.IdProfile == idProfile);
The results were totally different from what I expected - instead of getting single Profile with rights I got single profile with single right!
How can I fix it?
You can use like this
var profile = dc.Profiles.Where(p => p.IdProfile == idProfile).Select(x => x.Rights);

Kohana 3.2 ORM - relating models across multiple databases?

I have a non-standard database setup to work with and i'm trying to get this test case to work in Kohana 3.2 but not having any luck. Scenario:
I have a courses model using database A
I have a members model using database B
I have a courses_members join table in database A
Model for courses
class Model_Course extends ORM {
// Select the DB
protected $_db_group = 'default';
// override primary key
protected $_primary_key = 'courseid';
// Relationship
protected $_has_many = array(
'members' => array(
'model' => 'member',
'foreign_key' => 'memberID',
'through' => 'courses_members',
),
);
}
Model for members
class Model_Member extends ORM {
// Select the DB
protected $_db_group = 'alternate';
// override primary key
protected $_primary_key = 'memberID';
// Relationship
protected $_has_many = array(
'courses' => array(
'model' => 'course',
'foreign_key' => 'courseid',
'through' => 'courses_members'
),
);
}
Now in my controller trying i'm trying to echo out some test data
$courses = ORM::factory('course')->find_all();
foreach ($courses as $course)
{
echo $course->coursename . '<br/>';
foreach ($course->members as $member)
{
echo '-' . $member->username . '<br/>';
}
echo '<hr/>';
}
but $member->username results in an empty object. Empty objects make me sad.
Ideas? Can the Kohana ORM work this way across multiple databases?
This can be easily accomplished using the LEAP ORM for Kohana. In LEAP, all you have to do is specify in your models the data source (i.e. database config group) you want to use and the ORM will handle the switching between databases because it utilizes a database connection pool. Leap will also allow you to switch between database dialects since the module has drivers for DB2, Firebird, MariaDB, MS SQL, MySQL, Oracle, PostgreSQL, and SQLite. You can download the LEAP ORM from github at https://github.com/spadefoot/kohana-orm-leap.
It's a little late for the answer, but here is the solution. Change
foreach ($course->members as $member)
{
echo '-' . $member->username . '<br/>';
}
For:
foreach ($course->members->find_all() as $member)
{
echo '-' . $member->username . '<br/>';
}