Is it possible to make CRUD fields required ? In case i have NOT NULL fields in my database table i will get Integrity constraint violation error instead of simple client side required validation.
Just add in your rules() the fields you want to be required. See the Doc.
You can also use Gii to generate the models for you and edit what you want.
Related
I want to have a form where I can add new fields (columns) in an specific entity. Is there a function for this?
Kind regards
Adding a whole column to the table through an HTML form is a weird use case.
If you want to stick to the ORM way of managing the persisted data, you'll have to dynamically add properties to existing entities, which might be a sign of bad schema design.
What I would guess you probably need is an automated way to add this column to your Entity. In such a case I would use the maker bundle.
Supposing that your Entity is called Employee, all you have to do is to type in the following command:
bin/console make:entity
When you'll be asked for the Entity name, enter Employee. The interpreter will tell you that this entity exists and if you want to extend it with news fields, and there you go.
I have a table to manage many to many relationship (workers and skills)
Workers can have multiple skills and one skill can be assigned to multiple workers
What would be the best way to prevent duplicate entries so one skill is not assigned to the same worker twice?
thank you
If you have something like:
db.define_table('worker_skill',
Field('worker', 'reference worker'),
Field('skill', 'reference skill'))
To prevent duplicates via form submissions, you could then add a validator to one of the fields, such as:
db.worker_skill.skill.requires = IS_NOT_IN_DB(
db(db.worker_skill.worker == request.vars.worker), 'worker_skill.skill'
)
The above will ensure that the value being inserted in "skill" does not exist among the set of records where the value being inserted in "worker" matches the "worker" field.
Another option for form validation is use of an onvalidation callback, as explained in the forms chapter of the book.
You can also set a unique constraint on the pair of columns directly in the database (web2py does not handle that, so you will have to do that via an external tool). That will not help with form validation, as a violation of the constraint will simply result in the database driver throwing an exception (rather than presenting a friendly error message to the end user), but it will be useful if you are making inserts via means other than a web2py form.
I can quite easily generate a model from MySQL/MariaDB view with Gii, but when I try to generate CRUD, than I recieve the following error message:
The table associated with frontend\models\MyModel must have primary key(s).
See also the discussion in Yii Framework Forum.
The solution is:
add an ID in the view, e.g. using the CONCAT function,
overwrite the method primaryKey in the generated model.
Here is the code:
public static function primaryKey()
{
return array('view_id');
}
It is not a standard solution and should be used carefully. Yii does not officially support using active record with views, because different DBMS have different view specs and they usually don't support DB write (2).
Working on yii2,
There was issue with primary Key, so I search to add primary key in VIEW TABLE but I can't because -
Create view with primary key?
Adding in a primary key to an SQL view
Then moved to create CRUD on VIEW Table. I view many articles like-
http://www.yiiframework.com/forum/index.php/topic/9544-create-a-model-and-crud-from-mysql-view-instead-of-table/
https://code.google.com/archive/p/yii/issues/1274#c0
so finally,
public static function primaryKey()
{
return array('my_view_id');
}
Worked for me.
I'm using phpMyAdmin for my database GUI and it's connecting to Yii Framework on my website.
I wish for my products table for instance, to have a foreign key department_id which is a reference to the id field of my departments table. Unfortunately, I don't currently have the facility to set the foreign key up properly in phpMyAdmin, so department_id is just an indexed field. I cannot change the configuration of phpMyAdmin at the moment so it's stuck like it is without a foreign key and relationship facility.
Is there a way to modify the Models of these tables in Yii so they link? I know there is a relations function inside the Model Class file that holds this information:
return array('department_id' => array(self::BELONGS_TO, 'Departments', 'id'),
Could I not just add something similar to the above? Is there more legwork? Is it now fixed (as in static, not corrected) because of phpMyAdmin?
Cheers
If I'm not mistaken, you don't need to have mySql enforcing foreign key relationships for them to still work in Yii. Setting up FK constraints in mySql ensures proper database integrity, but I don't think Yii actually uses that at runtime.
When initially running yiic (of Gii) to build the project I think it looks at the DB to build the right relations in the Model, but it doesn't use them after that.
Yii then uses this knowledge (from yiic) of the table relationships to make your life easier by providing shortcut methods to access relational data, and to ensure you don't violate mySql constraints and get ugly SQL errors, etc. But you can still use Yii relation logic without the underlying SQL constraints. The only problem will be that if Yii messes up and assigns a non-existing FK or something, your database will not catch this error (your data integrity will be more error prone).
To link your products to departments, just make sure you have a department_id field in the Product (which it sounds like you do). Then add a relation rule like so to Product:
'department' => array(self::BELONGS_TO, 'Department', 'department_id'),
And in your Department model:
'products' => array(self::HAS_MANY, 'Product', 'department_id'),
Now you should be able to use the relation like normal:
$myProductModel->department; // returns the model of the Department referenced
$myDepartmentModel->products; // returns the models of all Products in the department
Good luck, and let me know if I am way off base and it doesn't work for you!
I have an interesting problem and wanted so see if anyone else has seen this. I've created a MVC 2 site using Visual studio 2010 beta 2. I'm using linq to sql data model objects with data annotations.
In my data model objects I'm using [ScaffoldColumn(false)] attribute to exclude the foreign key ID's from rendering to the UI when I use the EditorForModel method. For some reason the UI is rendering the foreign key table name.
e.g. if the foreign key is AccountID, i see the "account".
I wonder if this is a bug in the editorformodel or if I need to use a different/additional data annotation attribute to instruct editorformodel to not render anything.
You Linq to SQL classes will have a property for the foreign key itself (AccountID) as well as an EntitySet property for the related records in the Accounts table. If you open up the auto-generated designer.cs file under your linq to sql dbml - you can view all the properties of each class. I think by default the templated helpers are only supposed to generate an editor for the first level of properties. There is a "deep-dive" option that will extend the generated editor to more levels of properties. Check out Brad Wilson's blog on the subject (near the end of the post).
In general, trying to throw your auto-generated Linq to SQL classes into your View or annotate them with attributes gets pretty hairy. It might be worth checking into strongly typed view models where you could specify only the properties you're interested in displaying in your view.