How to insert fields from two models onto one form - php

I have done everything like in this example.
But I don't understand how to save the id fields to the related tables. I have two tables, company and entity, with one to one relations. It saves all the fields that I inserted, but not company_id and entity_id. Do I need to use aftersave?
In the Company controller:
public function actionCreate()
{
$model1=new Company;
$model2=new Entity;
if(isset($_POST['Company'], $_POST['Entity']))
{
// populate input data to $a and $b
$model1->attributes=$_POST['Company'];
$model2->attributes=$_POST['Entity'];
// validate BOTH $a and $b
$valid=$model1->validate();
//$valid=$model2->validate() && $valid;
if($valid)
{
// use false parameter to disable validation
$model1->save(false);
$model2->save(false);
// ...redirect to another page
}
}
$this->render('create', array(
'model1'=>$model1,
'model2'=>$model2,
));
}
In the Company model:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'entity'=>array(self::HAS_ONE, 'Entity', 'entity_id')
);
}

Ok, those are normal doubts when we begin playing with Yii. Let's see:
The "rules" method (in the models) should be only used to columns that are filled by the user. In your case, I guess "entity_id" is handled by the server, so remove it from "rules". With that new configuration, you can validate both objects before saving.
If validation succeeds, first save one model and use its "id" to save the other one that makes the reference. Something like that:
public function actionCreate()
{
$model1=new Company;
$model2=new Entity;
if(isset($_POST['Company'], $_POST['Entity']))
{
// populate input data to $a and $b
$model1->attributes=$_POST['Company'];
$model2->attributes=$_POST['Entity'];
// validate BOTH $a and $b
$valid=$model1->validate();
$valid=$model2->validate() && $valid;
if($valid)
{
$model2->save( );
$model1->entity_id = $model2->id;
$model1->save( );
}
}
$this->render('create', array(
'model1'=>$model1,
'model2'=>$model2,
));
}

Related

How to delegate "delete" operation to another Model in Cakephp4?

I have 2 Models (SuperRubriques and CustomRubriques) using the same table rubriques in DB.
When I delete from SuperRubriques, I would like to delegate the delete to CustomRubriques (as CustomRubriques has a hasOne association with extended_rubriques that SuperRubriques doesn't know).
For info, the rubriques table in DB has the field model containing 'CustomRubriques' (i.e. the Model with which it has been saved).
I've tried to do it in SuperRubriquesTable::beforeDete() :
// In SuperRubriquesTable.php
public function beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options)
{
$table = TableRegistry::getTableLocator()->get($entity->model); // $entity->model contains 'CustomRubriques'
$rubriqueEntity = $table->get($entity->id);
return $table->delete($rubriqueEntity);
}
However $table->delete($rubriqueEntity) is true (when I debug) but the record is not deleted in DB, I don't know why?
I've fix the issue :
Instead of deleguating inside SuperRubriquesTable::beforeDelete(), I've done the job in SuperRubriquesController::delete() :
// In SuperRubriquesController
public function delete($id)
{
$this->request->allowMethod(['post', 'delete']);
$rubrique = $this->SuperRubriques->get($id);
$rubriqueModel = $rubrique->model;// contains 'CustomRubriques'
$this->loadModel($rubriqueModel);
$rubriqueEntity = $this->$rubriqueModel->get($id);
if ($this->$rubriqueModel->delete($rubriqueEntity)) {
return $this->redirect(['action' => 'index']);
}
}

Laravel Polymorphic Relations commentable_type validation

I'm using a REST API to receive the data.
The data model is polymorphic related, similar to the one on the documentation:
https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Let's say, for example, the API is receiving this new comment:
{
"body": "This a test comment",
"commentable_type": "posts",
"commentable_id": "1"
}
How can I validate if the received commentable_type exists and is valid?
If I correctly understand your question, you are trying to validate that the object of the polymorphic relation exists, for the given commentable_type and commentable_id.
If that is the case, there is no existing validation rule to do so, but you can create one.
Based on the documentation, here is what you could do:
First, add the new rule in the boot method of a service provider (e.g. AppServiceProvider):
Validator::extend('poly_exists', function ($attribute, $value, $parameters, $validator) {
if (!$objectType = array_get($validator->getData(), $parameters[0], false)) {
return false;
}
return !empty(resolve($objectType)->find($value));
});
And this is how you would use it:
'commentable_id' => 'required|poly_exists:commentable_type
What the rule does is it tries and fetches the commentable type from the input values (based on the parameter passed on to the rule, i.e. commentable_type in our case), and then resolves the object and tries to find a record for the given ID ($value).
Please note that for this to work however, the value of commentable_type must be the fully qualified class name (e.g. App\Models\Post).
Hope this helps!
Better approach that includes morphs map:
Validator::extend('poly_exists', function ($attribute, $value, $parameters, $validator) {
if (! $type = array_get($validator->getData(), $parameters[0], false)) {
return false;
}
if (Relation::getMorphedModel($type)) {
$type = Relation::getMorphedModel($type);
}
if (! class_exists($type)) {
return false;
}
return ! empty(resolve($type)->find($value));
});
You can dynamically define a model_exists rule in your Request class. Something like this:
public function rules()
{
$polymorphExistsRule = '';
if ($this->has('commentable_type')) {
$polymorphExistsRule .= '|exists:' . $this->commentable_type . ',id';
}
return [
'commentable_type' => 'required_with:commentable_id',
'commentable_id' => 'required_with:commentable_type' . $polymorphExistsRule,
];
}
Edit
I might've misunderstood the first time. If you want to check that the model saved in commentable_type exists you could do something like this:
$type = $comment->commentable_type;
if(class_exists($type)) echo "it exists";
Depending on your needs you could do additional checking for it's inheritance (for example that it extends class Model). Or anything else that fits your needs really.
Edit2
This is what I would do if I were you. I would add property protected $allRelations to your Comment model and manually put all the relationships in. Then make some helper models to check if it's in the array.
Simple example:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
// ..
protected $allRelations= [
'posts' => '\App\Post',
'videos' => '\App\Video',
];
public static function validateRelationNs($ns) {
return in_array($ns, $this->allRelations);
}
public static function validateRelationName($name) {
return array_key_exists($name, $this->allRelations);
}
// ...
}
Old answer:
Laravel expects full namespace name of the model for polymorphic type columns (in your case commentable_type should be \Full\Ns\Post, not posts).
The easiest way to ensure correctness is to always save it through the relationship. For example:
$post = Post::first();
$comment = new Comment($attributes);
$post->comments()->save($comment).
This will automatically set both commentable_id and commentable_type correctly (assuming your relationsare correctly defined).
Additional checking
Other then that you could check through model events. You could validate it before saving to the database.
My final version work for validate type and id:
Validator::extend('poly_exists', function ($attribute, $value, $parameters, $validator) {
if (!$objectType = array_get($validator->getData(), $parameters[0], false)) {
return false;
}
if (!class_exists($objectType)) {
return false;
}
return !empty(resolve($objectType)->find($value));
});

Yii2 required validation on update

I've Yii2 form containing form fields depending on action of page. Ex. Few fields appears when then action is create and few appears when action is update. I want to add required validation based on this scenario.
Ex.
<?= $form->field($model, 'unique_identifier')->textInput(['maxlength' => 45]) ?>
I am showing this field only when action => 'update'.
Now I want to add required validation for this and I tried this:
[['unique_identifier'], 'required', 'on' => 'update'],
But above validation not working. If I remove on=>update then its validating on both create and update scenario.
Any help would be appreciated.
ActiveRecord does not set scenario automaticaly when you update or create items. You must override update() method in your model and set scenario that you need. E.g. in your case
public function update($runValidation = true, $attributeNames = null)
{
$this->scenario = 'update';
return parent::update($runValidation, $attributeNames);
}
Also you can set scenario in your actionUpdate
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->scenario = 'update';
//load data from request, save model etc.
}

Yii, changing original posted attributes

I'd like to ask, is it possible to change the original posted attributes in actionCreate()?
For example I have 3 attributes: name, phNumber, address
In the _form.php, it automatically posts these 3 attributes. BUT what if I want to change the posted name attribute to all Uppercases? Do I need to create my own method of creating a record just to change how the name will be recorded OR is there something that I can do in actionCreate() so that it only changes the name attribute?
For example, user types in
adam michael
for the name textbox, and I want to change only this attribute to
ADAM MICHAEL
to be recorded in the database instead of having to create another method.
Code below:
public function actionCreate() {
$model = new Masseuse;
if (isset($_POST['Masseuse'])) {
$model->setAttributes($_POST['Masseuse']);
if ($model->save()) {
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
else
$this->redirect(array('servicemasseuse/create', 'mid' => $model->id));
}
}
$this->render('create', array( 'model' => $model));
}
Just simply do a $model->name=strtoupper($model->name);
Refer here
You must alter the user input prior to saving the data. You do this by creating an overwritten function in your model.
class Masseuse extends CActiveRecord {
// ...
public function beforeSave()
{
$this->name = strtoupper($this->name)
}
}

kohana form validation

There is a 'main.php' view that contains a form with email and name fields and a submit button. Eveyrthing works fine with action_index (the code is below), but I'm curious how to modify the code below so it validates if the email was entered correctly. It should not put values in the database if the email field is not valid. I hope it is possible to made using ->rule. Is it? If yes, then how where to add the validation? (I had no luck trying it in different ways).
public function action_index()
{
if ( !empty($_POST) ) {
$model = ORM::factory('tbl1'); // create
$model->values($_POST); // load values to model
if ($model->check()) {
$model->save(); // save the model
} else {
//show errors
}
}
$this->response->body(View::factory('main'));
}
Thank you.
Use rules function in your ORM model:
public function rules()
{
return array(
'email' => array(
array('email', array(':value')),
),
);
}

Categories