I'm working with PageKit CMS. I have 2 tables with Many To Many relation (item and type).
Item model:
class Item implements \JsonSerializable
{
...
/**
* #ManyToMany(targetEntity="Type", tableThrough="#prefix_item_type", keyThroughFrom="item_id", keyThroughTo="type_id")
*/
public $types;
...
}
Type model:
class Type implements \JsonSerializable
{
...
/**
* #ManyToMany(targetEntity="Item", tableThrough="#prefix_item_type", keyThroughFrom="type_id", keyThroughTo="item_id")
*/
public $items;
...
}
In backend interface on item edit page I created multi select with all types. When I send item save request, I get type ids.
My save item method have a look:
public function saveAction($data, $id = 0, $selected_types = [])
{
/*
* $selected_types = array(2) {
* [0]=>int(1)
* [1]=>int(2)
* }
*/
if (!$id || !$item = Item::query()->related(['types'])) {
if ($id) {
App::abort(404, __('Item not found'));
}
$item = Item::create();
}
if (!$data['slug'] = App::filter($data['slug'] ?: $data['title'], 'slugify')) {
App::abort(400, __('Invalid alias'));
}
if(!App::user()->hasAccess('ext_name: manage all items')) {
$data['user_id'] = App::user()->id;
}
if(!App::user()->hasAccess('ext_name: manage all items') && !App::user()->hasAccess('ext_name: manage own items') && $item->user_id !== App::user()->id) {
App::abort(403, __('Access denied'));
}
$item->save($data);
/*
* Here I need to sync $item->types with $selected_types ids
*/
return [
'message' => 'success',
'entity' => $item,
];
}
How can I sync this relation if I have current item id and new type ids?
Related
last couple of days I've been busy making a form using Doctrine and MongoDB. Companies should be able to reserve tables, chairs, .. at a certain event by use of this form. The snippet below shows the controller for this form.
The 'ObjectMap' object maps the amount of a certain object to the object itself. The controller creates all the 'ObjectMap' objects, and adds them to the company object. However, the 'ObjectMap' objects are persisted by Doctrine (they show up in the database) but the company object isn't modified at all, there is no database request made by MongoDB. The persist() function seems to have no effect at all.
public function logisticsAction(Company $company)
{
$form = $this->createForm(new LogisticsForm($this->getDoctrine()->getManager()), $company);
if ($this->getRequest()->isMethod('POST')) {
$form->bind($this->getRequest());
if ($form->isValid()) {
$formData = $this->getRequest()->request->get('_company_logistics_edit');
$objects = $this->getDoctrine()->getManager()
->getRepository('Jobfair\AppBundle\Document\Company\Logistics\Object')
->findAll();
foreach($objects as $object) {
$requirement = $formData['objectRequirement_'.$object->getId()];
$map = new ObjectMap($requirement, $object);
$this->getDoctrine()->getManager()->persist($map);
$company->addObjectMap($map);
//print_r($company->getObjectMaps());
}
$this->getDoctrine()->getManager()->persist($company);
$this->getDoctrine()->getManager()->flush();
$this->getRequest()->getSession()->getFlashBag()->add(
'success',
'The information was successfully updated!'
);
return $this->redirect(
$this->generateUrl(
'_company_settings_logistics',
array(
'company' => $company->getId(),
)
)
);
}
}
The Company object is defined here:
class Company{
/**
* #ODM\Id
*/
private $id;
/*
* #ODM\ReferenceMany(targetDocument="Jobfair\AppBundle\Document\Company\Logistics\ObjectMap", cascade={"persist", "remove"})
*/
private $objectMaps;
public function __construct($name = null, $description = null)
{
$this->objectMaps = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getObjectMaps()
{
return $this->objectMaps;
}
public function getObjectsArray()
{
$objects = array();
foreach($this->objectMaps as $map)
$objects[] = array(
'name' => $map->getObject()->getName(),
'amount' => $map->getRequirement()
);
return $objects;
}
public function addObjectMap(ObjectMapDocument $objectMap)
{
$this->objectMaps[] = $objectMap;
}
public function removeObject(ObjectMapDocument $objectMap)
{
$this->objectMaps->removeElement($objectMap);
}
}
I've been trying for countless hours now, but still having issues updating a models relationship, the closest I've got to is a 'Method fill does not exist.' error.
Listing model:
class Listing extends Model
{
protected $fillable = [
'uid', 'start_date',...........
];
public function locations()
{
return $this->hasMany('App\ListingLocation');
}
}
Location (relationship to listing - hasMany):
class ListingLocation extends Model
{
protected $fillable = [
'listing_id', 'location',
];
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
This returns my model and relationship, which I can view with dd($listing)
$listing = Listing::with('locations')->findOrFail($id);
This will update my listing model, which I can see the changes after calling dd($listing) again
$listing->fill($array);
However when I attempt to fill the relationship as per below, I get 'Method fill does not exist.'
$listing->locations->fill($array['locations']);
How can I update the relationship successfully before calling $listing->push();?
Change your location to a single record, not a collection
For example:
$listings->locations->first()->fill($array['locations']);
to fill every record use foreach
#foreach($listings->locations as $location)
$location->fill(do_something);
#endforeach
I ended up creating a new class to extend hasMany which allowed me to use sync as per alexweissman at https://laracasts.com/discuss/channels/general-discussion/syncing-one-to-many-relationships.
Extract from forum:
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* #link https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Relations/HasMany.php
*/
class HasManySyncable extends HasMany
{
public function sync($data, $deleting = true)
{
$changes = [
'created' => [], 'deleted' => [], 'updated' => [],
];
$relatedKeyName = $this->related->getKeyName();
// First we need to attach any of the associated models that are not currently
// in the child entity table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->newQuery()->pluck(
$relatedKeyName
)->all();
// Separate the submitted data into "update" and "new"
$updateRows = [];
$newRows = [];
foreach ($data as $row) {
// We determine "updateable" rows as those whose $relatedKeyName (usually 'id') is set, not empty, and
// match a related row in the database.
if (isset($row[$relatedKeyName]) && !empty($row[$relatedKeyName]) && in_array($row[$relatedKeyName], $current)) {
$id = $row[$relatedKeyName];
$updateRows[$id] = $row;
} else {
$newRows[] = $row;
}
}
// Next, we'll determine the rows in the database that aren't in the "update" list.
// These rows will be scheduled for deletion. Again, we determine based on the relatedKeyName (typically 'id').
$updateIds = array_keys($updateRows);
$deleteIds = [];
foreach ($current as $currentId) {
if (!in_array($currentId, $updateIds)) {
$deleteIds[] = $currentId;
}
}
// Delete any non-matching rows
if ($deleting && count($deleteIds) > 0) {
$this->getRelated()->destroy($deleteIds);
$changes['deleted'] = $this->castKeys($deleteIds);
}
// Update the updatable rows
foreach ($updateRows as $id => $row) {
$this->getRelated()->where($relatedKeyName, $id)
->update($row);
}
$changes['updated'] = $this->castKeys($updateIds);
// Insert the new rows
$newIds = [];
foreach ($newRows as $row) {
$newModel = $this->create($row);
$newIds[] = $newModel->$relatedKeyName;
}
$changes['created'][] = $this->castKeys($newIds);
return $changes;
}
/**
* Cast the given keys to integers if they are numeric and string otherwise.
*
* #param array $keys
* #return array
*/
protected function castKeys(array $keys)
{
return (array) array_map(function ($v) {
return $this->castKey($v);
}, $keys);
}
/**
* Cast the given key to an integer if it is numeric.
*
* #param mixed $key
* #return mixed
*/
protected function castKey($key)
{
return is_numeric($key) ? (int) $key : (string) $key;
}
}
You can then override Eloquent's hasMany method in your model class:
/**
* Overrides the default Eloquent hasMany relationship to return a HasManySyncable.
*
* {#inheritDoc}
*/
public function hasMany($related, $foreignKey = null, $localKey = null)
{
$instance = $this->newRelatedInstance($related);
$foreignKey = $foreignKey ?: $this->getForeignKey();
$localKey = $localKey ?: $this->getKeyName();
return new HasManySyncable(
$instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
);
}
/**
* Get all of a user's phone numbers.
*/
public function phones()
{
return $this->hasMany('App\Phone');
}
A sync method will now be available to any hasMany relationships you have on this model:
$user->phones()->sync([
[
'id' => 21,
'label' => "primary",
'number' => "5555551212"
],
[
'id' => null,
'label' => "mobile",
'number' => "1112223333"
]
]);
I am using fractal (fractal.thephpleague.com) to develop an API with Laravel (laravel.com). It is an amazing library, by the way.
In certain web service, I need to return information of several nested models, which have 3 levels deep. That is, I have a Survey model which has many Survey Items, and each one of them has, in turn, many Survey Item Results (each one of a user). Well, I need the data from all of them, classified, that is:
"surveys": [
{
"id": 1,
...,
"items": [
{
"id": 14,
...,
"results": [
{
"id": 45,
...
},
{
...
}
]
},
{
...
}
]
},
{
...
}
}
With transformers and includes, I get the surveys and survey items info without problems, but I also need the survey item results...
That is, I need something like 2-level "nested" includes, to get the information of the third level.
My best approach, so far (only returning two levels: surveys and survey items). In my controller:
return fractal() -> transform(
Survey::where(...),
new SurveyTransformer()
) -> include(['SurveyItems']) -> respond();
Any help is much appreciated.
Thanks in advance.
Here's what I normally do
Survey Transformer
<?php
namespace App\Transformers;
use League\Fractal;
use App\Survey;
class SurveyTransformer extends Fractal\TransformerAbstract
{
/**
* List of resources possible to include
*
* #var array
*/
protected $availableIncludes = [
'items'
];
public function transform(Survey $survey)
{
return [
'id' => (int) $user->id,
];
}
/**
* Include Items
*
* #param App\Survey $survey
* #return League\Fractal\CollectionResource
*/
public function includeItems(Survey $survey)
{
$items = $survey->items;
if (!is_null($items)) {
return $this->collection($items, new ItemTransformer);
}
return;
}
}
Item Transformer
<?php
namespace App\Transformers;
use League\Fractal;
use App\Item;
class ItemTransformer extends Fractal\TransformerAbstract
{
/**
* List of resources possible to include
*
* #var array
*/
protected $availableIncludes = [
'results'
];
public function transform(Item $item)
{
return [
'id' => (int) $user->id,
];
}
/**
* Include results
*
* #param App\Item $item
* #return League\Fractal\CollectionResource
*/
public function includeResults(Item $item)
{
$results = $item->results;
if (!is_null($results)) {
return $this->collection($results, new ResultTransformer);
}
return;
}
}
On my base controller
/**
* get fractal tranformed data
* #param $resource
*/
protected function fractalResponse($resource, array $includes = [])
{
$manager = new Manager();
$manager->setSerializer(new DataArraySerializer()); //or what ever you like
if (sizeof($includes) == 0) {
$data = $manager->createData($resource)
->toArray();
} else {
$manager->parseIncludes($includes);
$data = $manager->createData($resource)
->toArray();
}
return $data;
}
then
$resource = new \League\Fractal\Resource\Collection($survies, new SurveyTransformer);
$response_data = $this->fractalResponse($resource, ['items.results'])
An Order have many ordered items
An Order's ordered items can either be a User or Product
What I am looking for is a way to retrieve all morphed objects to an Order. Instead of $order->users or $order->products I would like to do $order->items.
My progress
My progress so far involves a Many To Many Polymorphic Relationship.
My tables:
orders
id - integer
orderables (the order items)
order_id - integer
orderable_id - integer
orderable_type - string
quantity - integer
price - double
-----------
users
id - integer
name - string
products
id - integer
name - string
Example on how orderables table look
This is how I create an order and add a user and a product:
/**
* Order
* #var Order
*/
$order = new App\Order;
$order->save();
/**
* Add user to order
* #var [type]
*/
$user = \App\User::find(1);
$order->users()->sync([
$user->id => [
'quantity' => 1,
'price' => $user->price()
]
]);
/**
* Add product to order
* #var [type]
*/
$product = \App\product::find(1);
$order->products()->sync([
$product->id => [
'quantity' => 1,
'price' => $product->price()
]
]);
Order.php
/**
* Ordered users
* #return [type] [description]
*/
public function users() {
return $this->morphedByMany('Athliit\User', 'orderable');
}
/**
* Ordered products
*/
public function products() {
return $this->morphedByMany('Athliit\Product', 'orderable');
}
Currently I can do
foreach($order->users as $user) {
echo $user->id;
}
Or..
foreach($order->products as $product) {
echo $product->id;
}
But I would like to be able to do something along the lines of...
foreach($order->items as $item) {
// $item is either User or Product class
}
I have found this question, which was the closest I could find to what I am trying to do, but I can't make it work in regards to my needs, it is outdated, and also seems like a very hacky solution.
Have a different approach?
If you have a different approach than Polymorphic relationships, please let me know.
Personally, my Order models have many OrderItems, and it is the OrderItems that have the polymorphic relation. That way, I can fetch all items of an order, no matter what type of model they are:
class Order extends Model
{
public function items()
{
return $this->hasMany(OrderItem::class);
}
public function addItem(Orderable $item, $quantity)
{
if (!is_int($quantity)) {
throw new InvalidArgumentException('Quantity must be an integer');
}
$item = OrderItem::createFromOrderable($item);
$item->quantity = $quantity;
$this->items()->save($item);
}
}
class OrderItem extends Model
{
public static function createFromOrderable(Orderable $item)
{
$this->orderable()->associate($item);
}
public function order()
{
return $this->belongsTo(Order::class);
}
public function orderable()
{
return $this->morphTo('orderable');
}
}
I’ll then create an interface and trait that I can apply to Eloquent models that makes them “orderable”:
interface Orderable
{
public function getPrice();
}
trait Orderable
{
public function orderable()
{
return $this->morphMany(OrderItem::class, 'orderable');
}
}
use App\Contracts\Orderable as OrderableContract; // interface
use App\Orderable; // trait
class Product extends Model implements OrderableContract
{
use Orderable;
}
class EventTicket extends Model implements OrderableContract
{
use Orderable;
}
As you can see, my OrderItem instance could be either a Product, EventTicket, or any other model that implements the Orderable interface. You can then fetch all of your order’s items like this:
$orderItem = Order::find($orderId)->items;
And it doesn’t matter what type the OrderItem instances are morphed to.
EDIT: To add items to your orders:
// Create an order instance
$order = new Order;
// Add an item to the order
$order->addItem(User::find($userId), $quantity);
I think your solution is fine. I'd just add this helper method:
Order.php
public function items() {
return collect($this->products)->merge($this->users);
}
Then you can loop through the items with:
foreach ($order->items() as $item) {
I am am trying to save to my database, and as part of that save I am trying to sync my many to many relationship, however I am getting the following error from my API,
"BadMethodCallException","message":"Call to undefined method Illuminate\\Database\\Query\\Builder::sync()"
I would have thought that this is because the relationships I have in my model are not many to many so cant be synced, but they look correct to me,
class Organisation extends Eloquent {
//Organsiation __has_many__ users (members)
public function users()
{
return $this->belongsToMany('User')->withPivot('is_admin');
}
//Organisation __has_many__ clients
public function clients()
{
return $this->belongsToMany('Client');
}
//Organisation __has_many__ teams
public function teams()
{
return $this->belongsToMany('Team');
}
//Organisation __has_many__ projects
public function projects()
{
return $this->hasMany('Project');
}
}
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
public function organisations()
{
return $this->belongsToMany('Organisation')->withPivot('is_admin');
}
}
I am running the sync after a successful save,
if(isset($members)) {
$organisation->users()->sync($members);
}
and members is certainly set. The organsisation is created in the following way,
public function create()
{
//
$postData = Input::all();
$rules = array(
'name' => 'required',
);
$validation = Validator::make(Input::all(), $rules);
if($validation->fails()) {
return Response::json( $validation->messages()->first(), 500);
} else {
$organisation = new Organisation;
// Save the basic organistion data.
$organisation->name = $postData['name'];
$organisation->information = $postData['information'];
$organisation->type = 'organisation';
/*
* Create an array of users that can used for syncinng the many-to-many relationship
* Loop the array to assign admins to the organisation also.
*/
if(isset($postData['members'])) {
$members = array();
foreach($postData['members'] as $member) {
if(isset($postData['admin'][$member['id']]) && $postData['admin'][$member['id']] == "on") {
$members[$member['id']] = array(
'is_admin' => 1
);
} else {
$members[$member['id']] = array(
'is_admin' => 0
);
}
}
}
/*
* Create an array of clients so we can sync the relationship easily
*
*/
if(isset($postData['clients'])) {
$clients = array();
foreach($postData['clients'] as $client) {
$clients[] = $client['id'];
}
}
/*
* Create an array of teams so we can sync the relationship easily
*
*/
if(isset($postData['teams'])) {
$teams = array();
foreach($postData['teams'] as $team) {
$teams[] = $team['id'];
}
}
/*
* Create an array of projects so we can sync the relationship easily
*
*/
if(isset($postData['projects'])) {
$projects = array();
foreach($postData['projects'] as $project) {
$projects[] = $project['id'];
}
}
if( $organisation->save() ) {
if(isset($members)) {
$organisation->users()->sync($members);
}
if(isset($teams)) {
$organisation->teams()->sync($teams);
}
if(isset($teams)) {
$organisation->clients()->sync($clients);
}
if(isset($projects)) {
$organisation->projects()->sync($projects);
}
$organisation->load('users');
$organisation->load('teams');
$organisation->load('clients');
$organisation->load('projects');
return Response::make($organisation, 200);
} else {
return Response::make("Something has gone wrong", 500);
}
}
}
I was looking a while for the problem and I didn't see any (I was looking at first sync as you suggested) but I looked again and I think the problem is not syncing users here. Probably the problem is:
if(isset($projects)) {
$organisation->projects()->sync($projects);
}
You are trying to use sync on 1 to many relationship because you defined it this way:
return $this->hasMany('Project');
So either change hasMany here into belongsToMany if it's many to many relationship (that's probably the case) or don't use sync here for $projects because it works only for many to many relationship.