How saving function in CakePHP - cakephp-3.0

Edit :
I have a function for generate a reference, in my controller and I'd save it returns at the same time add that my form is saved, but I would avoid spending the variable view and make a hidden input, how can I do? I try to use the model but without success!
public function afterSave(Event $event, Entity $entity){
if($this->Adverts->save()){
$ref = $this->ref();
$ref = $this->Adverts->reference;
}
}

Related

How to create a model object in yii2 by string name?

I need to create a model by string name that it is a variable.
function($modelName){
$modelName= "backend\\models\\".$modelName;
$modelClass = Yii::createObject([
'class' => $modelName,
]);
$model = $modelClass::find();
}
when I pass Book(it is extracted form DB) as modelName to function, it throws an error: Class backend\models\Book does not exist.
but when I write $modelName= "backend\\models\\Book"; it works fine.
I know it is because of run time and compile time. but I don't know how to solve it. because $modelName is Characterized at run time.
You are accessing to a static method using an object. You should access to the static method just using the class name eg:
$modelName = 'backend\models\\' . $modelName;
$model = $modelName::find();
And remember that $modelName::find() don't return a model but just the query object for a model. To obtain a model you should use eg: $modelName::find()->where(['id'=>$your_value])->one();

Yii2 how can an model attribute is modified after load method? (like the afterFind method)

I have an attribute of the model which should be modified after it's loaded from the database.
I could extend the afterFind method, which could the convert the varchar value to a php array. So it works find.
But when the model is loaded I have no idea how to convert that varchar to the php array.
I have tried with rules but does not works:
[['languages'], 'each', 'rule' => ['string']],
or this one
[['languages'], 'safe'],
So this one works afterFind:
public function afterFind()
{
$this->languages = $this->convertToPHPArray($this->languages);
parent::afterFind();
}
By the way I have tried to extend the init or the __constructor method with this conversation, but no success, after load method the languages attribute is still a string instead of a php array.
If I understood your question, I think that you could use a property in the model:
public class Model {
public function getLanguagesArray()
{
return $this->convertToPHPArray($this->languages);
}
}
Then, use it:
$arr = $model->languagesArray;

Custom Task : TaskHost parameters persistence

I am currently developing a custom task and I'm stuck on a problem.
My Custom Task consists of three files:
MBTask that contains the class that implements the interface "Task"
MBTaskUI that implements the interface "IDtsTaskUI"
MBForm which is a Form.
In MBTaskUI on the Initialize() function i reveive the TaskHost object and save it in a variable. And then in the GetView() function o send this TaskHost to my Form builder
public void Initialize(TaskHost taskHost, IServiceProvider serviceProvider)
{
this.taskHost = taskHost;
IDtsConnectionService cs = serviceProvider.GetService
(typeof(IDtsConnectionService)) as IDtsConnectionService;
this.connections = cs.GetConnections();
}
public ContainerControl GetView()
{
return new MBForm(this.taskHost, this.connections);
}
It work great and i can use this TaskHost in my Form to get parameters when i load the Form and save them when I close it.
String script_tmp = (String)th.Properties["myScript"].GetValue(th);
SqlConnection conn_tmp = (SqlConnection)th.Properties["myConnection"].GetValue(th);
th.Properties["myScript"].SetValue(th, myScript);
th.Properties["myConnection"].SetValue(th, myConnectionTarget);
And so, when i re-open my Task Editor, i use the parameters i received to initialize some objects.
But, and here is my problem, in my MBTask class, i have a Validate() function which verify the parameters (myConnection and myScript). and these parameters are always null (also after I normally initialized it by using the MBForm and the close function.
So, what I don't understand is why my parameters were not instanced ?
Isn't it the job of "th.Properties["myScript"].SetValue(th, myScript);" ?
myScript and myConnection are declared public in my MBTask.
public class MBTask : Task
{
public SqlConnection myConnection { get; set; }
public String myScript { get; set; }
I know these parameters are saved because i can get them back when i re-open the editor. So why are they null when I do the Validate() ?
Do I have to add a link to the TaskHost from the MBTaskUI and the one from the MBTask ?
It seems that there is something i don't understand here ...
Thanks for your help,
Léo Lejeune.
I fond the solution:
The system I implemented was correct. But if you want to use complex parameters (SQLConnection) you have to use XML saving.
So, to use the default saving parameter of the task, only use simple objects like String and Integer.
And the Validate function can't use parameters because they are not yet instanced and so they are null.

Doctrine 2 namespace issue

I'm using Zend Framework 1 with the Bisna library to integrate Doctrine 2. I generated my Entities from my database model with the Doctrine 2 CLI. This is all working fine, except for the setter methods for associated records. The argument they accept must be of a specific namespace (\Category here).
class Article
{
public function setCategory(\Category $category = null) {
$this->category = $category;
return $this;
}
}
However, when I do this:
$article = $this->em->getRepository('\Application\Entity\Article')->find(1);
$category = new \Application\Entity\Category();
$category->SetName('New Category');
$article->setCategory($category);
I get the following fatal error: Argument 1 passed to Application\Entity\CategoryField::setCategory() must be an instance of Category, instance of Application\Entity\Category given.
When I change the setter method to accept \Application\Entity\Category objects, it's working of course. Should I do this for every generated method, or are there other options? This is the first time I'm using namespaces, so it might be something simple.
You can always add this to the top of your class file: use \Application\Entity\Category; and then simply reference it later like so: public function setCategory(Category $category = null)
Check out: http://php.net/manual/en/language.namespaces.importing.php for more info about use
Otherwise you would have to reference the full namespace otherwise your application does not know that \Category is a reference to \Application\Entity\Category

Set Header as 'application/json' to all actions in controller YII

I am creating a rest api using yii framework so the basic output format would be json....
I want all actions in a controller to have header content-type as 'application-json'.
I tried to put it in beforeFilter function in the controller but it didn't work.
Can any one help me out...
Create an init() function in the Controller class (protected/components/Controller.php). This will be called when any Controller/Action is called. Eg:
public function init(){
if ($this->id == 1){
// perform controller specific function
}
}
The $this->id returns the controller id. You must obviously replace the 1 in the code above with the relevant controller id for the controller you want the function to take place with