Magento2 - setData() For Added Fields - php

I have added a field(key) in table(quote_item) through module installation(InstallSchema) and in Api Code I am using the following code to add value for that. But its not adding the value to that field in DB.
//cartItemObject \Magento\Quote\Model\Quote\Item
$cartItemObject->setData("key", "value");
Can anyone please help me on this?

Before making the setData, did you start the model? If you are using factory you should do as follows:
$cartItemObject=$this->model->create(); // after this you would have access to the following methods getData(), setData(), etc
In any case if it still does not work I advise you to review this information for handling model in magento 2

Related

How Symfony works to remove an collection items

When I submit a collection with removed elements, my elements are correctly removed but, I don't understand how Symfony remove elements.
When I die(); the script doesn't stop into my remover method, so I think Symfony doesn't pass into my method.
So, how Symfony can remove my elements without call my remover method ? How it's work ?
Application context :
I got some data from an API and I just want to add or remove data from my web interface. So, after I've add/remove data, I just want to post the new object on the API and remove the rows concerned. I also can compare the original object (get from the API) on pre submit and the new object on post submit to see the differences between both. But, if a method exist to call the remover of my object, I don't have to use this second solution and I think the first solution with a personnal remover is better.
Example of my remover :
public function removeMyElement($myElement)
{
personnalMethodTreatment($myElement);
if (!$this->myElement->contains($myElement)) {
return;
}
$this->myElement->removeElement($myElement);
}
Thank's for your help !
SOLUTION
So, thank to all for the help.
I write this lines to help other people who would have the same problem.
By default, Symfony call this own remover. If you want to call a custom remover, add the by_reference option at false to your collection form type.
by_reference: false
More information about by_reference https://symfony.com/doc/current/reference/forms/types/collection.html#by-reference
To conclude, by default Symfony call your custom method in this order :
Setter > Remover
So, if you have a setter and a remover, Symfony will prefer the former.
To solve my issu, I've followed the precedent instructions and I maked my specific treatment into my setter.
Hoping to have helped people.

Call function/add own code after Order in Opencart

i'm working on Open Cart 3.0.2.0.
I want to call my own function after someone buy a product from my store. Where i can call this function? I tried to put the test code in catalog/model/checkout/order.php in addOrder and addOrderHistory methods, also in catalog/controller/chekout/confirm.php but it doesn't work.. Can anyone know in which file and method i should put my own code?
There is piece of my test code:
<?php
class ModelCheckoutOrder extends Model {
public function addOrder($data) {
//TEST
file_put_contents('my_directory/test.txt', 'test');
...
Edit: I know that in older versions was Confirm method i Checkout Model, and there u could pass your code, but it disappeared in new versions.
I can put my code in catalog/controller/success.php, but i need to get and pass informations about all ordered products. Any ideas?
Best regards.
I found the answer few days ago. The first option is get order id from session in "success.php" file and work with it.
Or (second option) you can edit files in the system/storage/modification !

ATK4 Autocomplete issue

I'm new to ATK4.
I'm trying to implement an Autocomplete field, but I had only an error when I try to define the field.
I'm running all on a MAC with the last version (4.2.4) of ATK4. All other functions (field types) seems to work well, but when I define a field of type autocomplete I get the same error I saw on the example: http://codepad.agiletoolkit.org/autocomplete
The case is I defined a model:
class Model_Agenda extends Model_Table {
public $entity_code='Agenda';
function init(){
parent::init();
$this->addField('DATE');
$this->addField('TIME');
$this->addField('DRIVERID');
$this->addField('STUDENTID');
$this->hasOne('STUDENTID')->display(array('form'=>'autocomplete/Basic'));
}
}
and then on my Page:
$form = $this->add('Form');
$form->addField('ReadOnly','Date')->Set($_GET['date']);
$form->addField('ReadOnly','Time')->Set($_GET['time']);
$form->addField('ReadOnly','Driver')->Set($_GET['driverid']);
$client=$form->addField('autocomplete','studentid');
something simple, but nothing, I cannot get the autocomplete field, I ever get an error that say that "autocomplete.php" doesn't exist (actually the file doesn't exist and I try to download the github module, but either that module includes this file), anyway the error I get is:
Exception_PathFinder, code: 0
Additional information:
file: Form/Field/Autocomplete.php
type: php
attempted_locations:
0: /Library/WebServer/Documents/rutas/lib/Form/Field/Autocomplete.php
1: /Library/WebServer/Documents/rutas/atk4/lib/Form/Field/Autocomplete.php
2: /Library/WebServer/Documents/rutas/atk4-addons/mvc/Form/Field/Autocomplete.php
3: /Library/WebServer/Documents/rutas/atk4-addons/misc/lib/Form/Field/Autocomplete.php
class: Form_Field_Autocomplete
namespace:
orig_class: Form_Field_Autocomplete
/Library/WebServer/Documents/rutas/atk4/lib/PathFinder.php:207
Someone could help me please?
You're talking about this add-on, right: https://github.com/atk4/autocomplete?
Looks like issue with PathFinder unable to find appropriate location of namespaced addons.
Please post here part of your API_Frontend class where you add additional locations to pathfinder and also tell me something more about your folder structure (in which folder you have put autocomplete add-on files).
ATK 4.2.4 version is not last. Can you try to upgrade ATK to version 4.2.5? It's not officially released, but is available in GitHub master branch here: https://github.com/atk4/atk4?
I hope everything will work with 4.2.5 version but if not, then I'll help you solve this issue.
And one more thing - if you're creating form fields manually, then you need to set Model for that $client field. I guess it can be done with $client->setModel('Agenda');
EDIT: correct answer
In line
$client=$form->addField('autocomplete','studentid');
you should write full autocomplete field class name with namespace like this:
$client=$form->addField('autocomplete/Basic', 'studentid');
One more thing to note is to use lowercase function name. So instead of Set() use set().

Adding a field specific error from the controller in symfony2

I have some complex validation going on with my symfony form, and I need to be able to assign an error to a specific field from my controller. Right now, I have global errors working like this:
$error = new formerror("There is an error with the form");
$form->addError($error);
But that creates a global error, not one bound to a specific field.
Is there a way to throw an error on a specific field from my controller?
Thanks to some help over IRC (thanks #fkrauthan!) I came up with an answer.
Every field in SF2 is actually an instance of form. What you need to do is access the form object of the field, and add then error onto it. Thankfully, symfony provides a method to get an embedded form/field.
Heres my code:
$error = new FormError("There is an error with the field");
$form->get('field')->addError($error);
As some people have pointed out, you will need to include the FormError class at the top of your file:
use Symfony\Component\Form\FormError;

How to add new rules in Kohana framework

I'm using Kohana framework for my project development, I need to introduce new rules Suppose I have original_price and discount_price fields, for this discount_price must be always always less than or equal to original_price.
For this scenario, how to write new rule(method).
In your Kohana framework,you will find validation.php file,in that write a appropriate validation function.Suppose you want to test some length of a text field then write relevant function here and call that function in your controller class using add_rules().I hope you got the answer.
Use v3.2 and check this page out: http://docs.kohanaphp.com/libraries/validation

Categories