sonata disallow delete if an entity is linked to another - sonata-admin

i need to disallow delete function in an entity if is linked to another
for example there are two entity Product and Category, and Category is linked to Product with a relationship oneToMany or ManytoMany
i want disallow the delete function of an element in Category if this element is linked in a product element
is possible?
thank you

You can check if the ArrayCollection contains more than 0 entities. If that is the case block deletion of the entity by using some sort of validation. Since you didn't specify wether you're submitting an admin, form or block. I will assume this is for an admin class.
There is an chapter about validation within admin classes in the Sonata Documentation. Within the validate(ErrorElement $errorElement, $object) you can check $object for the collection and amount of entities in the colleciton.

Related

How to programmatically identify and list fields with unique=True in any given model

My friends,
How would I do to identify, in any given model, which fields have the parameter unique=True? And how about unique_together. Is there a way to programmatically identify fields with this characteristics?
Any instance from any model have a method called _get_unique_checks() that will return a list of tuples with all the fields that are marked as unique.
Similarly, any instance of any model have another method called _check_unique_together().

Is class diagram required to change if DB contains Views on tables?

I'm a newbie in designing class diagrams. I follow DAO pattern in my design.
In my project, one user can add multiple contacts which can further be added to many groups.
Let's say 1 contact -> many groups.
As per my requirement UserContact and UserGroup are classes which manage contacts and groups.
In DB contacts and groups are stored in two different tables.
There is one use case to retrieve all contacts along with their groups so that I need to retrieve all contacts first and then using contactID again I need to make a query to get its relative groups.To avoid this in DB, there is VIEW on these two tables.
Now,with VIEW, I need to make one query to get user contacts and groups.
How can I add this method in DAO?
How do I change my classes so that contacts and groups can be mapped to my objects?
The following are the classes involved in this use case.
If I understand the question correctly; you can create a GroupContactDAOImpl class that has only query (get) methods, e.g. getGroupContacts(groupId) or getAllGroupContacts() if you will. Since "group contacts" is not an entity that has its own table, UserContactDAOImpl.addContact and UserGroupDAOImpl.addGroup methods would be sufficient to add new records.
If I need to get groups associated with one contact, how can I map contact information and groups to one object?
If you want to map contact info and groups to one object, you can add a UserGroup property to UserContact class (like in Hibernate). Then GroupContactDAOImpl will have a method like: UserContact getUserContactWithGroups(contactID).
Similarly, if you'd like to get a group's info with all the contacts asscoiated; you can add a UserContact property to UserGroup class. GroupContactDAOImpl will have a method like: UserGroup getUserGroupWithContacts(groupID).
There might be other methods to do the same thing; hope this helps.
As I need to insert and update a contact using UserContact, it should contain only contactID, email and name. I use UserContact and UserGroup as DTOs. Can I create a new class which holds UserContact and UserGroup that uses UserContactDAOImpl and is it a preferable way?
Yes, you can follow that way if you'd like to leave UserContact class as it is. You'd have a new DTO (e.g UserContactWithGroups) and return it from UserContactDAOImpl (e.g .getContactWithGroups method.) You can also create a separate DAO class, but it doesn't matter much. It's best to adapt it to your own use case.

CakePHP 2.1 HABTM Not Getting Associated Info

I have a question that's been bugging me all afternoon:
I'm making a guitar gear site, so I am using a gear items table, a user table, and gear-to-user bridge table. That is, a user can own multiple items and items can belong to multiple users. So HABTM.
An item belongs to a brand (e.g. Fender - Stratocaster), so I set up a belongsTo relationship in the item model as well as a HasMany relationship in the brands model. When I check the output in the items controller, the gear and its associated brand's data is all there as it should be.
The user control panel (and similar areas) basically list all of the user's owned items. After setting up the HABTM relationship between users and items, I checked the controller's output. While the item's information and the bridge table information all appeared, the item's associated brand information did not. The results should essentially be a list of items, including brand information, as if it were "where user_id = x". Instead, it seems to only be grabbing the item information and none of its relationships.
Is there something I'm missing or a dumb mistake? Thanks.
Did you consider setting/changing the recursive attribute when performing find() on the User?
The higher recursive is set, the deeper associations will be found.
An alternative might be to use the Containable behavior on User:
$this->User->find('first',array(
'conditions'=>array('User.id'=>1),
'contain' => array('Item'=>array('Brand'))
));
Set the recursive level up or you can use ContainableBehavior

Create multiselect lookup in salesforce using apex

I want to create a multi-select Contact Lookup.
What i want :
When user clicks on a lookup then he should be able to select multiple contacts from that.
What i have done:
I have created an object and a field inside that object using both
"Lookup" and
"MasterDetail Relationship" and
"Junction Object"
When i try to use this Field for any input text/Field then it always provides an option to select only one value from lookup but i want to have an option to select multiple.
Even in the Junction object i have created 2 master-detail relationships still lookup allows only one value to be selected.Moreover it makes the field mandatory which i don't want.
Links that i followed:
http://success.salesforce.com/questionDetail?qId=a1X30000000Hl5dEAC
https://ap1.salesforce.com/help/doc/user_ed.jsp?loc=help&section=help&hash=topic-title&target=relationships_manytomany.htm
Can anybody suggest me how to do this.
Its same as we use Email CC/BCC under Send Email option for any Lead.
Even you use a junction object a lookup is just that, it references (looks up to) one other record: when you create a record on the junction object you still have to set each lookup individually and you're still creating only one record.
Master Detail relationships are essentially lookups on steroids, one object becomes the child of the other and will be deleted if the parent object is deleted, they're not going to provide an interface to lookup to many records at once.
If you're not a developer then your best bet is to either just create on junction object record at a time, or look into using dataloader. You could prepare your data in Excel or similar and then upload all the records into Salesforce in one go.
If you are a developer, or have developers at your disposal, then what we've done in the past is create a Visualforce page to do the job. So if, for example, you wanted to link a bunch of contacts up to an Account, we'd have a single account lookup field on the page, then some search fields relating to fields on the contact. Using a SOQL query you can then find all contacts matching the search parameters and display them in a list, where you may want to provide checkboxes to allow the user to select the contacts they want. Then it's just a case of looping through the selected contacts, setting their Account field to be the chosen account.
There are areas in Salesforce (such as the send Email functionality you mentioned) where it's clear to see that bespoke work has been done to fulfil a specific task — another instance of what you want is in the area where you can manage campaign members. This is the model I've copied in the past when implementing a Visualforce page as described.
Good luck!
For adding multiple junction objects at one time, the only solution we have found is a custom Visualforce page, as described by LaceySnr.
For a slightly different problem, where we need to assign many of object B to object A, We have trained our users to do this with a view on object B. We are assigning Billing Accounts (B) to Payment Offices (A). The view on Billing Account has check boxes on the left side. The user checks the Billing Accounts to be assigned, then double-clicks on the Payment Office field on any of the checked rows. A pop-up asks if you want to update only the single row or all checked rows. By selecting 'all checked rows', the update is done to all of them.
The view is created by the user, who enters the selection criteria (name, address, state, etc.). All user-created views are visible only to them.

Cakephp retrieve data in HABTM association

I have 2 tables subscribers and distribution list and the relationship between them is HABTM. Now here particular users are associated to distribution list, i wish to add more users, but when adding new users to distribution list, i would like to show users who are not associated with that distribution lists. what condition should i write
$this->DistributionSubscriber->find('list', array('conditions'=>array('distribution_id <>' => 1), 'fields'=>array('subscriber_id'), 'recursive'=>-1));
I would try something like this. You want to start with the join table in this case, and do your find() from there. I haven't checked this syntax, but you should get the idea.

Resources