How to load the model in magento 2
like
Mage::getModel('catalog/product')->load()
as we used to get in magento 1.
It is recommended to use dependency injection rather than directly using object Manager. Example:
In your block file you can use following code to return product collection:
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->_productCollectionFactory->create();
return $collection;
}
Try this for get all product collection
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
->addAttributeToSelect('*')
->load();
foreach ($collection as $product){
echo 'Name = '.$product->getName().'<br>';
}
?>
Related
I am trying to write a unit test for FormErrorSerializer that converts Symfony $form->getErrors() to a readable array.
My current approach is to create the form, give it data, and look for validation errors, but form is always valid. I don't get any errors no matter what data I provide to form.
In normal REST request/response it is working well and I am getting appropriate error message. I need help with getting the error messages in unit test.
namespace App\Tests\Unit;
use App\Form\UserType;
use App\Serializer\FormErrorSerializer;
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Translation\Translator;
class FormErrorSerializerTest extends TypeTestCase
{
/**
* ValidatorExtensionTrait needed for invalid_options
* https://github.com/symfony/symfony/issues/22593
*/
use ValidatorExtensionTrait;
public function testConvertFormToArray(){
$form_data = [
'email' => 'test',
'plainPassword' => [
'pass' => '1',
'pass2' => '2'
]
];
$translator = new Translator('de');
$form = $this->factory->create(UserType::class);
$form->submit($form_data);
if( $form->isValid() ) {
echo "Form is valid"; exit;
}
$formErrorSerializer = new FormErrorSerializer($translator);
$errors = $formErrorSerializer->convertFormToArray($form);
print_r($errors); exit;
}
}
Find below the Serializer:
namespace App\Serializer;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Serializes invalid Form instances.
*/
class FormErrorSerializer
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function convertFormToArray(FormInterface $data)
{
$form = $errors = [];
foreach ($data->getErrors() as $error) {
$errors[] = $this->getErrorMessage($error);
}
if ($errors) {
$form['errors'] = $errors;
}
$children = [];
foreach ($data->all() as $child) {
if ($child instanceof FormInterface) {
$children[$child->getName()] = $this->convertFormToArray($child);
}
}
if ($children) {
$form['children'] = $children;
}
return $form;
}
private function getErrorMessage(FormError $error)
{
if (null !== $error->getMessagePluralization()) {
return $this->translator->transChoice(
$error->getMessageTemplate(),
$error->getMessagePluralization(),
$error->getMessageParameters(),
'validators'
);
}
return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators');
}
}
Ok, I was able to do this in 2 different ways.
First solution was to load the validator in getExtensions method. The factory in TypeTestCase doesn't bring the validator with it. So, not only you have to load the validator but you also have to explicitly specify the validations. You can specify validation using methods provided by symfony or you can directly point validator to the YAML or xml file if you are using one.
public function getExtensions()
{
$validator = (new ValidatorBuilder())
->addYamlMapping("path_to_validations.yaml")
->setConstraintValidatorFactory(new ConstraintValidatorFactory())
->getValidator();
$extensions[] = new CoreExtension();
$extensions[] = new ValidatorExtension($validator);
return $extensions;
}
However, I didn't use the above approach. I went with even better solution. Due to high complexity of my test case (as it needed multiple services), I went with a special container provided by Symfony's KernelTestCase. It provides private services in tests, and the factory it provides comes with validator and validations, just like you code in controller. You do not need to load validator explicitly. Find below my final test that extends KernelTestCase.
namespace App\Tests\Unit\Serializer;
use App\Entity\User;
use App\Form\UserType;
use App\Serializer\FormErrorSerializer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Translation\TranslatorInterface;
class FormErrorSerializerTest extends KernelTestCase
{
/**
* {#inheritDoc}
*/
protected function setUp()
{
$kernel = self::bootKernel();
}
public function testConvertFormToArray_invalidData(){
$form_data = [
'email' => 'test',
'plainPassword' => [
'pass' => '1111',
'pass2' => ''
]
];
$user = new User();
$user->setEmail($form_data['email']);
$user->setPlainPassword($form_data['plainPassword']['pass']);
$factory = self::$container->get(FormFactoryInterface::class);
/**
* #var FormInterface $form
*/
$form = $factory->create(UserType::class, $user);
$form->submit($form_data);
$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());
$translator = self::$container->get(TranslatorInterface::class);
$formErrorSerializer = new FormErrorSerializer($translator);
$errors = $formErrorSerializer->convertFormToArray($form);
$this->assertArrayHasKey('errors', $errors['children']['email']);
$this->assertArrayHasKey('errors', $errors['children']['plainPassword']['children']['pass']);
}
public function testConvertFormToArray_validData(){
$form_data = [
'email' => 'test#example.com',
'plainPassword' => [
'pass' => 'somepassword#slkd12',
'pass2' => 'somepassword#slkd12'
]
];
$user = new User();
$user->setEmail($form_data['email']);
$user->setPlainPassword($form_data['plainPassword']['pass']);
$factory = self::$container->get(FormFactoryInterface::class);
/**
* #var FormInterface $form
*/
$form = $factory->create(UserType::class, $user);
$form->submit($form_data);
$this->assertTrue($form->isSubmitted());
$this->assertTrue($form->isValid());
$translator = self::$container->get(TranslatorInterface::class);
$formErrorSerializer = new FormErrorSerializer($translator);
$errors = $formErrorSerializer->convertFormToArray($form);
$this->assertArrayNotHasKey('errors', $errors['children']['email']);
$this->assertArrayNotHasKey('errors', $errors['children']['plainPassword']['children']['pass']);
}
}
Please note that Symfony 4.1 has a special container that allows fetching private services.
self::$kernel->getContainer(); is not special container. It will not fetch private services.
However, self::$container; is special container that provides private services in testing.
More about this here.
Trying to get POST parameters in doctrine listener. But, getting an empty array. Tried to push the request to requeststack in controller as well. But, still coming in as empty.
class OrderListener
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $this->getObject();
if ($entity instanceof Order) {
$request = $this->requestStack->getCurrentRequest();
$this->handleEvent($entity, $args->getEntityManager());
}
}
public function handleEvent(Order $order, EntityManagerInterface $em)
{
// here $request->request->all() is empty
$request = $this->requestStack->getCurrentRequest();
$transaction = new Transaction();
$transaction->setOrder($order);
$transaction->setRemoteId($request->request->get('stripeInvoiceId'));
...
}
}
So, json_decode($request->getContent(), true) gave me the correct params. Returned an array of post data. Strange thing is $request->request->all() should have worked too.
i'm trying to update an object by using this code :
The column co_nbre will be updated to 0 !!!!
I think you will help me to fix this issue and thnx a lot.
public function update($model) {
$data = get_object_vars($model);
$id = (int) $model->id;
$this->tableGateway->update($data, array('id' => $id));
}
and this is how did i use it in my controller:
if ($form->isValid()) {
$data = $form->getData();
$addi_info = new Addiinfo();
$addi_info->exchangeArray($data);
$addi_info->co_nbre = $request->getPost("co_nbre");
$addi_info->user_pin = $this->layout()->pin;
$addi_info->co_latitude = $request->getPost("latitude");
$addi_info->co_longitude = $request->getPost("longitude");
$addi_info->co_adresse = $request->getPost("adresse");
print_r($addi_info);die;
$checkuser=$this->getAddiinfoTable()->getAddiInfoByUserPin($user_pin);
if($checkuser[user_pin]==$user_pin){
$this->getAddiinfoTable()->update($addi_info);
I think you should create a function that returns associative array from model itself.
May be some of property in "Addiinfo" class be protected/private, so you need to get all property-value of model from inside it.
This one should be in your "Addiinfo" class,
public function getArrayData()
{
return get_object_vars($this);
}
Then call it in update function
public function update($model) {
$data = $model->getArrayData();
$id = (int) $model->id;
$this->tableGateway->update($data, array('id' => $id));
}
I have a form :
$houserent = new Zend_Form_Element_Text('houserent');
$houserent ->setLabel('House Rent :');
$this ->addElement($houserent);
$tax = new Zend_Form_Element_Text('tax');
$siteName ->setLabel('Tax :');
$this ->addElement($tax);
$internet = new Zend_Form_Element_Text('internet');
$internet->setLabel('Internet :');
$this ->addElement($internet);
and my data table "test" is
id name value
1 house rent 100
2 tax 10
3 internet 10
I want to populate the form using the above data from tha database table. But I don't know how to this. Please help me. Thanks
You can overwrite the populate function in your form.
public function populate($data)
{
foreach($data as $field => $value)
{
$this->{$field}->setValue($value);
}
return $this;
}
where $data is an associated array of name => value.
[edit]
So you form is now:
<?php
class Form_Admin_Settings_Add extends Zend_Form
{
public function init()
{
$houserent = new Zend_Form_Element_Text('houserent');
$houserent->setLabel('House Rent :');
$this->addElement($houserent);
$tax = new Zend_Form_Element_Text('tax');
$siteName->setLabel('Tax :');
$this->addElement($tax);
$internet = new Zend_Form_Element_Text('internet');
$internet->setLabel('Internet :');
$this->addElement($internet);
}
public function populate($data)
{
foreach($data as $field => $value)
{
$this->{$field}->setValue($value);
}
return $this;
}
}
In your controller:
<?php
//instantiate form and model
$form = new Form_Admin_Settings_Add();
$model = new Model_Test();
//get results
$results = $model->fetchAll()->toArray();
$data = array();
//put results into our data array as name => value
foreach($results as $r)
{
$data[$r['name']] = $r['value'];
}
//populate our form
$form->populate(data);
echo $form;
Try some like this:
class Form extends Zend_Form {
public funcition Form(){
$houserent = new Zend_Form_Element_Text('houserent');
$houserent->setLabel('House Rent :')
->setValue($this->_Data['houserent']);
$this->addElement($houserent);
}
public function setData($Data){
$this->_Data = $Data;
return $this;
}
}
At the moment I have a page where I have retrieved information on a club by the id of that club. I now have a comments box where I want to retrieve the comments about that club, in the comments table I have the club_id and the parameter "club_id" is passed into this page. At the moment I am retrieving all of the comments from the table but I want just the comments for that club. A point in the right direction would be great!
Controller:
class ClubDescriptionController extends Zend_Controller_Action
{
public $auth = null;
public function init()
{
$this->auth=Zend_Auth::getInstance();
}
http://pastebin.com/m66Sg26x
protected function authoriseUser()
{
if (!$this->auth->hasIdentity()) {
$route = array('controller'=>'auth', 'action'=>'index');
$this->_helper->redirector->gotoRoute($route);
}
}
}
Model:
class Application_Model_DbTable_Comments extends Zend_Db_Table_Abstract
{
protected $_name = 'comments';
public function getComment($id) {
$id = (int) $id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Count not find row $id");
}
return $row->toArray();
}
public function addComment($comment, $club_id) {
$data = array(
'comment' => $comment,
'club_id' => $club_id,
'comment_date' => new Zend_Db_Expr('NOW()'),
);
$this->insert($data);
}
public function deleteComment($id) {
$this->delete('id =' . (int) $id);
}
}
The view:
<div id="view-comments">
<?php foreach($this->comments as $comments) : ?>
<p id="individual-comment">
<?php echo $this->escape($comments->comment);?> -
<i><?php echo $this->escape($comments->comment_date);?></i>
</p>
<?php endforeach; ?>
</div>
I realise I am going to have to use the getComment(); function in my model and query it by the id but I'm getting confused on exactly how...
Thanks
It's been a while since I used Db_Table but I think you want to create a select object, which allows you to build a query that will select comments with the correct club_id:
$comments = new Application_Model_DbTable_Comments();
$select = $comments->select();
$select->where('club_id = ?', $id);
$this->view->comments = $comments->fetchAll($select);
you may want to order the comments by date, if so, you can do this by adding an order clause to the select:
$select->order('comment_date ASC');
take a look at the docs for Zend_Db_Table_Select, which has quite a few examples: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.fetch-all
In your controller you are calling
$this->view->comments = $comments->fetchAll();
it should be
$this->view->comments = $comments->getComment($this->_request->getParam('club_id'));
where id variable will be fetched from url.
Here is the working controller:
public function indexAction() {
//authorisation
$this->authoriseUser();
//to get the paramter club_id to query for specific club information
$id = (int) $this->_request->getParam('club_id', 0);
//submit a comment
$form = new Application_Form_Comment();
$form->submit->setLabel('Comment');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$comment = new Application_Model_DbTable_Comments();
$comment->addComment($formData['comment'], $id);
} else {
$form->populate($formData);
}
}
//initialise table
$clubs = new Application_Model_DbTable_Clubs();
$clubs = $clubs->getClub($id);
$this->view->clubs = $clubs;
//to get the comments for the club
$comments = new Application_Model_DbTable_Comments();
$select = $comments->select();
$select->where('club_id = ?', $id);
$select->order('comment_date ASC');
$this->view->comments = $comments->fetchAll($select);
}