Symfon2: How to set default values (options) for Entity field type? - forms

I am creating a form which uses an Entity type.
The entity form type displays Roles as a list of checkboxes.
But I dont know how to set default values. I need to get default values from DB then dynamically check some of those options.
According to the documentation, It seems like 'preferred_choices' option won't do this job.
Can anyone please help me out there?
Sorry about my English if some sentences don't make sense.
3 Tables:
UserRole
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| role_id | int(11) | YES | MUL | NULL | |
+---------+---------+------+-----+---------+----------------+
AdminUser
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(25) | NO | UNI | NULL | |
| salt | varchar(32) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| email | varchar(60) | NO | UNI | NULL | |
| is_active | tinyint(1) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
Role
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | UNI | NULL | |
+-------+-------------+------+-----+---------+----------------+
Form builder:
$builder->add('role', 'entity', array(
'class' => 'AcmeAdminBundle:Role',
'property' => 'name',
'multiple' => TRUE,
'expanded' => TRUE,
));

You must have defined a ManyToMany relation between User and Role, with some traditional methods on User entity : setRoles, getRoles, addRole...
Data that will be loaded in your form are data from a User instance, for example $user.
$user = new User; // or $user is existing User, same logic
$rolesYouWantToSetToUser = array('ROLE_1', 'ROLE_2', 'ROLE_3');
foreach ($rolesYouWantToSetToUser as $roleId) {
// $em must previsouly be set as EntityManager in your code
$role = $em->getReference('YourBundle:Role', $roleId);
$user->addRole($role);
}
// From a controller
$form = $this->createFormBuilder($user)
->add('roles', 'entity', array(
'class' => 'AcmeAdminBundle:Role',
'multiple' => true,
'expanded' => true,
'property' => 'name',
))
->getForm();

Related

How to add a validation on delta table column dynamically?

I'm working on a transformation and stuck with a common problem. Any assist is well appreciated.
Scenario:
Step-1: Reading from a delta table.
+--------+------------------+
| emp_id | str |
+--------+------------------+
| 1 | name=qwerty. |
| 2 | age=22 |
| 3 | job=googling |
| 4 | dob=12-Jan-2001 |
| 5 | weight=62.7. |
+--------+------------------+
Step-2: I'm refining the data and outputting it into another delta table dynamically (No predefined schema). Let's say I'm adding null if the column name is not found.
+--------+--------+------+----------+-------------+--------+
| emp_id | name | age | job | dob | weight |
+--------+--------+------+----------+-------------+--------+
| 1 | qwerty | null | null | null | null |
| 2 | null | 22 | null | null | null |
| 3 | null | null | googling | null | null |
| 4 | null | null | null | 12-Jan-2001 | null |
| 5 | null | null | null | null | 62.7 |
+--------+--------+------+----------+-------------+--------+
Is there a way to apply validation in step-2 based on the column name? I'm splitting it by = while deriving the above table. Or do I have to do validation in step-3 while working on the new df?
Second question: Is there a way to achieve the following table?
+--------+--------+------+----------+-------------+--------+---------------------+
| emp_id | name | age | job | dob | weight | missing_attributes |
+--------+--------+------+----------+-------------+--------+---------------------+
| 1 | qwerty | null | null | null | null | age,job,dob,weight |
| 2 | null | 22 | null | null | null | name,job,dob,weight |
| 3 | null | null | googling | null | null | name,age,dob,weight |
| 4 | null | null | null | 12-Jan-2001 | null | name,age,job,weight |
| 5 | null | null | null | null | 62.7 | name,age,job,dob |
+--------+--------+------+----------+-------------+--------+---------------------+

Postgres: return true/false if matches found from inner join?

I have implemented a tagging system in my application, using Postgres 9.6. There are three tables.
Projects
Table "public.project"
Column | Type | Collation | Nullable | Default
-------------+-----------------------------+-----------+----------+---------------------------------
id | integer | | not null | nextval('tag_id_seq'::regclass)
name | character varying(255) | | not null |
user_id | integer | | |
Tags
Table "public.tag"
Column | Type | Collation | Nullable | Default
-------------+-----------------------------+-----------+----------+---------------------------------
id | integer | | not null | nextval('tag_id_seq'::regclass)
tag | character varying(255) | | not null |
user_id | integer | | |
is_internal | boolean | | not null | false
Project tags
Column | Type | Collation | Nullable | Default
------------------+-----------------------------+-----------+----------+-----------------------------------------
id | integer | | not null | nextval('project_tag_id_seq'::regclass)
tag_id | integer | | not null |
project_id | integer | | | |
user_id | integer | | not null |
Now I want to get a list of all the projects, annotated with a column that indicates (for a particular tag) whether it has that tag.
So I'd like the results to look like this:
id name has_favorite_tag
1 foo true
2 bar false
3 baz false
This is my query so far:
select project.*, CASE(XXXX) as has_project_tag
from project p
join (select * from project_tag where tag_id=1) pt on p.id=pt.project_id
I know that I want to use CASE to be true when the length of project_tag matches is greater than 0 - but how do I do this?
(In reality the project table has many more fields, of course.)
Here's a possibility (unfiltered for tag_id; add to inner select if necessary):
select project.*, exists(select * from project_tag where id=project.id) as has_project_tag from project;

FuelPHP SimpleAuth custom column name?

With SimpleAuth package, Im working on login function.
By default, user table is supposed to have a column "username".
But my system has a user table with "login_id" instead of "username".
Fuel\Core\Database_Exception [ 1054 ]:
Unknown column 'username' in 'where clause'
[ SELECT * FROM `m_user` WHERE (`username` = 'admin' OR `email` = 'admin')
AND `password` = 'mIIMXQgANGZ21XHRTpI2Krpla9yx3UwEw0PCNfjAN4I=' ]
I cannot figure out how it handles custom field name.
I tried configuration on APPPATH/config/simpleauth.php with "'username_post_key' => 'login_id'," which did not work..
Thanks in advance.
### APPPATH/config/auth.php ###
return array(
'driver' => 'SimpleAuth',
'verify_multiple_logins' => false,
'salt' => 'techtech',
'iterations' => 10000,
);
### APPPATH/config/simpleauth.php ###
<?php
return array(
'db_connection' => null,
'table_name' => 'm_user',
'table_columns' => array('*'),
'guest_login' => true,
'multiple_logins' => false,
'remember_me' => array(
'enabled' => false,
'cookie_name' => 'rmcookie',
'expiration' => 86400 * 31,
),
'groups' => array(
),
'roles' => array(
),
'login_hash_salt' => 'tech_login',
'username_post_key' => 'login_id',
'password_post_key' => 'password',
);
### user table ###
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| login_id | varchar(100) | NO | | NULL | |
| first_name | varchar(20) | NO | | NULL | |
| last_name | varchar(20) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| last_login_at | timestamp | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
You can't configure it, because username_post_key is for POST field name, not for table column name. There is no configuration for table column name.
You must customize SimpleAuth, or create your own driver.

An incompatible mapping has been encountered between [class Entity.Classification] and [class Entity.Cls_area_map]

I'm new to JPA and I am having some difficulty creating some entities.
In the application I am building it is possible to classify some entities according to some area and subarea defined in a database.
The relevant tables are these four ones:
1) classification
+-------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------------------+------+-----+---------+----------------+
| ID | int(11) unsigned | NO | PRI | NULL | auto_increment |
| pID | int(11) unsigned | NO | MUL | NULL | |
| reference | varchar(300) | NO | | NULL | |
| link | varchar(255) | YES | | NULL | |
+-------------+-----------------------+------+-----+---------+----------------+
2) cls_area_map
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| class | int(11) unsigned | NO | MUL | NULL | |
| idarea | int(11) unsigned | NO | MUL | NULL | |
| subarea | int(11) unsigned | YES | MUL | NULL | |
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
+---------+------------------+------+-----+---------+----------------+
3) area
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| idarea | int(11) unsigned | NO | PRI | NULL | auto_increment |
| label | varchar(255) | NO | UNI | NULL | |
+--------+------------------+------+-----+---------+----------------+
4) subarea
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| area_idarea | int(11) unsigned | NO | MUL | NULL | |
| label | varchar(255) | NO | UNI | NULL | |
| ID | int(11) unsigned | NO | PRI | NULL | auto_increment |
+-------------+------------------+------+-----+---------+----------------+
In classification I store general classification information, and in cls_area_map I try to connect the general information to the areas of classification (defined in area and subarea).
When I try to add the classification-area mapping information to my Classification and my Cls_area_map entity classes I run into trouble.
I get the error:
An incompatible mapping has been encountered between [class Entity.Classification] and [class Entity.Cls_area_map]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer..
I'm not sure what I'm doing wrong about the cardinality. This is what I added to Classification to create the relationship:
#OneToMany(mappedBy = "id")
private List<Cls_area_map> cls_area;
and in Cls_area_map:
#JoinColumn(name = "class",referencedColumnName = "ID")
#ManyToOne(optional=false)
private Classification classy;
Any explanations/hints?
(and what is meant by backpointer?)
mappedBy indicates that the entity in this side is the inverse of the relationship. So entity name should be used instead of foreign key.
The doc says
mappedBy refers to the property name of the association on the owner
side.
It is classy in your case, so use
#OneToMany(mappedBy = "classy")
private List<Cls_area_map> cls_area;
See also:
mappedBy

Zend_Acl, with roles and permissions stored in database

i want to build an ACL system for my application which have the following requirement.
Users will be assigned single or multiple role. (Admin, Staff) etc.
Role will have permissions.(Send_Invoices, Send_mail, Delete_Invoices, Send_Estimate) etc.
User will be assigned custom permission apart from the role it inherits.
my database structure for ACL is as follows
role:
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| roleName | varchar(50) | NO | UNI | NULL | |
+----------+-------------+------+-----+---------+----------------+
permission:
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| permissionName | varchar(50) | NO | | NULL | |
| permissionKey | varchar(50) | NO | UNI | NULL | |
+----------------+-------------+------+-----+---------+----------------+
role_permission
+---------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| role_id | int(11) | NO | MUL | NULL | |
| permission_id | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+----------------+
user_role
+---------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| role_id | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+----------------+
user_permission
+---------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| permission_id | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+----------------+
i have migrated to Zend Framework, and having problem deciding wether Zend_Acl allows me to implement the current structure. my question is.
is it possible for me to implement the ACL with current database structure to do the needful in Zend Framework?
is there any better implementation that could allow me to achieve what i want in zend framework?
i will be grateful if someone could provide me a way to get started with what i need to do. any resources, links that could help me?
thank you.
Well I think this structure is really good , to get this working you had to do 2 steps
1-Setup all the databases and requirements
2- create an ACL plugin that determine the user's role and his permissions
some example with doctrine support :
Developing a Doctrine-backed ACL helper TDD-style, part 1
Developing a Doctrine-backed ACL helper TDD-style, part 2
another simple ACL :
Dynamic custom ACL in zend framework?
Why not use the Grants/Roles system already in the database?