symfony2 forms and many-to-many relations - php

I thought I'd see if anyone could point me in the right direction here - I have a users table and a contents table, and I want to keep track of what user is authorized to edit what content.
Thinking in a relational database way, a many-to-many relation table is the solution, adding rows of user_id & content_id for each authorization.
Many users can be authorized to edit a content piece, and a user is also of course able to have access to more than one content piece.
Now, the biggest issue I see (being a Symfony2 novice) is the admin form where an admin must be able to set these permissions, preferrably with a list of contents and checkboxes next to them, and have the form processor take care of the adding/deleting rows to that relationsl table.
Is the collection form field the right way to go here?
Any help is much appreciated.

I think the Doctrine2 documentation on Many-to-Many relations already gives you an answer on that question how to map the data.
The form issue is already explained in another SO question.

Related

PHP - Insert as draft

I'm building a CRM and I have a list of customers.
When I create a customer, I usually insert it as a draft as soon as the user clicks on "Insert button". Why? Because I want the user to be able to insert photos and documents on the insertion form. This way, I have the customer ID already and I can process the upload using that ID.
The problem here is that we can have multiple drafts at a certain moment.
My question is: is there any better method to do this? Without creating the draft? For instance, using a temp folder where the photos and documents are temporarily saved?
EDIT:
the issue woth having multiple drafts is that at certain point will may have a lot of trash.
I mean, this works for me ok? And it's a solution I have, I just wanted to know if there's a better solution instead of using drafts.
It depends on what exactly you are using to save the customer in the database.
If you are using an ORM such as Eloquent or Doctrine you just need to have the correct relationships between Entities and you won't need to save the Customer as draft, you can just flush all of the information at once.
If you aren't using an ORM, you can still do it in one request, just insert the Customer record first and get the last inserted ID , for example if using PDO, you can use PDO::lastInsertId https://www.php.net/manual/en/pdo.lastinsertid.php .
After you have the ID just set it to the other columns where you need it.

How to add only one data in database table in laravel

I am creating a site and i am making a general option page for the site that include the site name, logo, social links so that i can manage them dynamically from one place without changing the links and text in all pages where i called them.
What I want is that the table i created in database, i want to have only one row in it. If someone try to add other row of data it should not be added because i have to call only one data for the site.
I hope the query is clear to all.
Thanks for your support. Help me if u can. please :)
By,
if someone try to add other row of data it should not be added
what do you mean? Are you trying to avoid unauthorized access? If yes, then you need to implement roles https://laracasts.com/discuss/channels/general-discussion/roles-and-permissions-in-laravel-5
To manage your site information, I'm sure you set a form that will be used to update the site info. And from the look of things you will only be updating that row when you hit save or update button. I don't think you will be doing full CRUD here just updating so there'll be no chance of inserting a new row of data in the db.
So three things:
you want to create a migration, seed the table with the site details accordingly. run the migration
you want to make sure the user trying to access this route is authorized to do so
you want to pre populate the site info in the form fields when the edit site info page loads and you want to save the form when the authorized user changes any of the site info
This way, there'll be no question of stopping further insertions into the db.

Laravel multiple relations, save either one or the other

So I am wondering about the best approach for this issue. I have an appointments table and two relations, a contact relation and a company relation. In my appointments.create form, I want to be able to either select a contact, or a company, not both. I was thinking of two approaches but I hope there is a better way to accomplish this.
I can enter a question in my form, asking if the user wants to add a contact or a company, and if they select contact, I will display a contact dropdown, and the same for when the user selects company. In my appointments table I will have two fields, a contact_id and a company_id. I save whichever the user selected and set the other to either 0 or null. Then in my controller I select which was filled, and then grab the correct relation.
I can use the same field in my database for both, for example relation_id and then add another field to which I can save the text contact or company, and based on that I grab the correct relation in my views.
But both of these solutions seem like non solutions to me, and I am hoping there is a better way to achieve this, and since I have struggled with this a lot in the past as well, I thought it would be good to ask it here since I haven't been able to find a solution.

php mysql one to many relationship or something else?

I am trying to figure out what to do from here. I am using mysql for my database.
In my form from mysql I wish to have the possibility for people to upload from 1 to many images that can be queried along with the data submitted in the form. I am not asking for someone to write me the code, but to point me in the right direction of what type of solution I can use or what I can read on to learn to get this done.
User example here:
1. a registered user is filling out a form for example a car for sale ad.
2. user wishes to upload images relating to that car sale ad.
3.if the user fills out the car for sale form and clicks submit/next.How to display the form the user just clicked submit in the next page to appear,so the user can add images there/here?
I hope someone can point me in the right direction or can give me their input on what else I can read on to do this.
Thank you in advance
Personally when I start having to get into complex HABTM relationships and the what I want to use an ORM. I personally use CakePHP's. It has a very robust relational mapping system (HasMany, HasOne, BelongsTo, HasAndBelongsToMany) which is a bit of a pain to learn to do in SQL your self.
If you don't have access to a server with the right php modules 3.0 can be a pain to install, but 2.0 has a really nice RubyOnRails style ActiveRecord system too.

Tag-like skills in a user profile with a HABTM relationship

In the web application I'm building a user has a profile where they can list their skills. I would like the kind of functionality StackOverflow has when making posts, where you can type tags into the tag input and select ones that already exist, and create them if they don't already exist.
At the moment, I've got a select box appearing on the page with with the id of the users current skills as values. I'm achieving this by doing:
// ProfilesController.php
$skills = $this->Profile->ProfilesSkill->find('list');
// edit.ctp
<?php echo $this->Form->input('Skill', array('value' => $skills)); ?>
I've got no idea how to progress further, though. First of all, the name field for the skills should be shown instead of their id, which I'm confused about because by Cake's convention it will use the name field by default, even though it's not. And secondly when I enter my skills into my profile Cake should automatically make all the required entries in the profiles_skills table. How can I make that work?
The solution is pretty straight forward, though it's pretty much work.
First you'll need a Tag-System. You could build one on your own (like any habtm-relation), or use a plugin like https://github.com/CakeDC/tags
For the second part, the function is called "Autocomplete". It's basically a ajax call every time you enter a letter in a form field. There are a bunch of tutorials out there, e.g: http://blogfreakz.com/cakephp/cakephp-jquery-autocomplete-tutorial/
Hope this points you in the right direction

Categories