I have a problem with my FormType. I want to display data and thanks to the querybuilder make a distinction. Problem when I call my method with a -> select ('t.nomVern') I have "Warning: spl_object_hash () expects parameter 1 to be object, string given" as an error message.
I do not understand why.
My FormType ObservationType:
<?php
namespace ObservationBundle\Form;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
use ImportBundle\Repository\TaxrefRepository;
use ImportBundle\Entity\Taxref;
class ObservationType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date', DateType::class, array(
'widget' => 'single_text',
'html5' => false,
'attr' => array(
'placeholder' => 'Choisir la date de l\'observation',
)
))
->add('latitude', TextType::class, array(
'attr' => array(
'placeholder' => 'Latitude ex : 31.85322'
)
))
->add('longitude', TextType::class, array(
'attr' => array(
'placeholder' => 'Longitude ex : 33.55555'
)
))
->add('nombre', IntegerType::class)
->add('imageFile', VichImageType::class, array(
'required' => false,
'allow_delete' => false, // not mandatory, default is true
'download_link' => false, // not mandatory, default is true
'attr' => array(
'placeholder' => 'Votre image'
)
))
->add('valide', HiddenType::class)
->add('commentaire', HiddenType::class)
->add('gpsAffiche', HiddenType::class)
->add('meteo', HiddenType::class)
->add('saison', HiddenType::class)
->add('typeSaisie', HiddenType::class)
->add('precipitation', HiddenType::class)
->add('periode', HiddenType::class)
->add('environnement', HiddenType::class)
->add('sensibilite', HiddenType::class)
->add('comportement', HiddenType::class)
->add('species', EntityType::class, array(
'label' => 'Espèce observée :',
'class' => 'ImportBundle\Entity\Taxref',
'choice_label' => 'nomVern',
'query_builder' => function(TaxrefRepository $qb){
return $qb->distinctTaxref();
}
))
;
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ObservationBundle\Entity\Observation'
));
}
/**
* #return string
*/
public function getBlockPrefix()
{
return 'observationbundle_observation';
}
}
And my repository :
<?php
namespace ImportBundle\Repository;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class TaxrefRepository extends EntityRepository
{
/**
* Pagination liste des especes
* #param int $page
* #param int $max
* #return Paginator
*/
public function findByPage($page = 1, $max = 8)
{
if(!is_numeric($page)) {
throw new \InvalidArgumentException(
'$page must be an integer ('.gettype($page).' : '.$page.')'
);
}
if(!is_numeric($page)) {
throw new \InvalidArgumentException(
'$max must be an integer ('.gettype($max).' : '.$max.')'
);
}
$dql = $this->createQueryBuilder('t');
$dql->orderBy('t.id', 'DESC');
$firstResult = ($page - 1) * $max;
$query = $dql->getQuery();
$query->setFirstResult($firstResult);
$query->setMaxResults($max);
$paginator = new Paginator($query);
if(($paginator->count() <= $firstResult) && $page != 1) {
throw new NotFoundHttpException('Page not found');
}
return $paginator;
}
/**
* #return \Doctrine\ORM\QueryBuilder
*/
public function distinctTaxref()
{
return $this
->createQueryBuilder('t')
->select('t.nomVern')
->distinct(true)
->orderBy('t.nomVern', 'ASC');
}
}
Thank you in advance for your help and sorry for my bad english :/
Try this part of code
return $this
->createQueryBuilder('t')
->select(array('t.nomVern'))
->distinct(true)
->orderBy('t.nomVern', 'ASC');
I believe that, since you're using EntityType, Symfony expects to have an array of objects, whereas you select a single column which produces an array of strings.
Can you try selecting an object as a whole:
return $this
->createQueryBuilder('t')
->select('t')
->distinct(true)
->orderBy('t.nomVern', 'ASC');
Does this work?
Related
I am designing a module to calculate the VAT between two dates. I already have everything working, it already calculates the VAT perfectly, but now I want it to be displayed on the configuration screen of the module when clicking on save settings. It's the only thing I'm missing. Right now if I want the VAT to be shown I have to exit and re-enter the module configuration screen. Right now where I try to update the VAT field is in the postProcess() function, with this line:Configuration::updateValue($this->name . '_iva', $ivaAcumulado);
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class moduloResumenIva extends Module {
protected $config_form = false;
public function __construct() {
$this->name = 'moduloResumenIva';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'luilli';
$this->need_instance = 0;
/**
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('moduloResumenIva');
$this->description = $this->l('mi nuevo modulo mi nuevo modulomi nuevo modulomi nuevo modulomi nuevo modulo');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install() {
Configuration::updateValue('MIMODULOMISMADB_LIVE_MODE', false);
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('actionPaymentConfirmation') &&
$this->registerHook('backOfficeHeader');
}
public function uninstall() {
Configuration::deleteByName('MIMODULOMISMADB_LIVE_MODE');
return parent::uninstall();
}
/**
* Load the configuration form
*/
public function getContent() {
/**
* If values have been submitted in the form, process.
*/
if (((bool) Tools::isSubmit('submitButton')) == true) {
$this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');
return $output . $this->renderForm();
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm() {
$values = array();
$this->fields_form = array();
$this->context->controller->addJqueryUI('ui.datepicker');
$defaultDate = date('Y-m-d');
if (!Configuration::get($this->name . 'my_date_desde')) {
$values['my_date_desde'] = Tools::getValue('my_date_desde', $defaultDate);
} else {
$values['my_date_desde'] = Tools::getValue('my_date_desde', Configuration::get($this->name . '_my_date_desde'));
}
if (!Configuration::get($this->name . 'my_date_hasta')) {
$values['my_date_hasta'] = Tools::getValue('my_date_hasta', $defaultDate);
} else {
$values['my_date_hasta'] = Tools::getValue('my_date_hasta', Configuration::get($this->name . '_my_date_hasta'));
}
$values['iva'] = Tools::getValue('iva', Configuration::get($this->name . '_iva'));
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'Submit' . $this->name;
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $values,
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*/
protected function getConfigForm() {
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'date',
'label' => $this->l('Desde'),
'name' => 'my_date_desde',
'required' => true,
),
array(
'type' => 'date',
'label' => $this->l('Hasta'),
'name' => 'my_date_hasta',
'required' => true,
),
array(
'type' => 'text',
'label' => $this->l('Iva'),
'name' => 'iva',
)
),
'submit' => array(
'title' => $this->l('Save settings'),
'class' => 'button btn btn-default pull-right',
'name' => 'submitButton',
)
),
);
}
/**
* Set values for the inputs.
*/
protected function getConfigFormValues() {
return array(
'CAMPOID' => Configuration::get('CAMPOID', null),
'MIMODULOMISMADB_ACCOUNT_USUARIO' => Configuration::get('MIMODULOMISMADB_ACCOUNT_USUARIO', null),
'MIMODULOMISMADB_ACCOUNT_PASSWORD' => Configuration::get('MIMODULOMISMADB_ACCOUNT_PASSWORD', null),
'my_date_desde' => Configuration::get('my_date_desde', null),
'my_date_hasta' => Configuration::get('my_date_hasta', null),
'iva' => Configuration::get('iva', null),
);
}
/**
* Save form data.
*/
protected function postProcess() {
if (Tools::isSubmit('Submit' . $this->name)) {
if (Tools::getValue('my_date_desde')) {
Configuration::updateValue($this->name . '_my_date_desde', Tools::getValue('my_date_desde'));
}
if (Tools::getValue('my_date_hasta')) {
Configuration::updateValue($this->name . '_my_date_hasta', Tools::getValue('my_date_hasta'));
}
$fechaDesde = Configuration::get($this->name . '_my_date_desde', null) . " 00:00:00";
$fechaHasta = Configuration::get($this->name . '_my_date_hasta', null) . " 00:00:00";
$db = \Db::getInstance();
$sql = "select * from orders where date_add BETWEEN '" . $fechaDesde . "' AND '" . $fechaHasta . "'";
$ivaAcumulado = 0;
$result = $db->executeS($sql);
foreach ($result as $row) {
$ivaAcumulado += $row["total_paid_tax_incl"] - $row["total_paid_tax_excl"];
}
Configuration::updateValue($this->name . '_iva', $ivaAcumulado);
}
}
/**
* Add the CSS & JavaScript files you want to be loaded in the BO.
*/
public function hookBackOfficeHeader() {
if (Tools::getValue('module_name') == $this->name) {
$this->context->controller->addJS($this->_path . 'views/js/back.js');
$this->context->controller->addCSS($this->_path . 'views/css/back.css');
}
}
/**
* Add the CSS & JavaScript files you want to be added on the FO.
*/
public function hookHeader() {
//mail("luilli.guillan#gmail.com", "hola", "viva el vino");
$this->context->controller->addJS($this->_path . '/views/js/front.js');
$this->context->controller->addCSS($this->_path . '/views/css/front.css');
}
}
You can do the redirection after saving the changes:
https://github.com/PrestaShop/contactform/blob/dev/contactform.php#L99
if (((bool) Tools::isSubmit('submitButton')) == true) {
$this->postProcess();
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->name . '&conf=6');
}
As of now, I see that you don't have any validation, so this solution should be enough.
How can I use Laravel Faker to select from a predefined array of UUIDs?
class BookShelfFactory extends Factory
{
protected $model = BookShelf::class;
public function definition()
{
$uuids = array(
'9f86affa-66fc-11ed-9022-0242ac120002',
'e69e6546-d0a2-49e1-b4fe-2fe31de20252',
'c48c6318-dbd1-4881-ab77-ffee4e1fa3b1',
'f3031799-6cae-4edf-87a1-330f3e5d2a65',
'e8d7f5cc-71f5-4e05-848a-1cdab53c15de'
);
return [
'name' => $this->faker->text(50),
//'uuid' => $this->faker->uuid,
'uuid'=> $this->faker->sequence($uuids),
'author_id' => \App\Models\BookAuthor::factory(),
'created_by' => \App\Models\User::factory(),
];
}
}
Above code results in error: Unknown format "sequence
I am looking solutions for using different categories for main and mobile Magento store view. I have configure mobile store view as new theme with user agent string and exception.
How can I do show one categories on main store view and another one categories on mobile store view. Both store views use by one domain name.
What I will suggest it create an category attribute with dropdown. Following Script will help you to do so :
SQL Setup file :
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("catalog_category", "wheretoshow", array(
"type" => "int",
"backend" => "",
"frontend" => "",
"label" => "Where to Show",
"input" => "select",
"class" => "",
"source" => "modulename/eav_entity_attribute_source_categoryoptions",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
"visible" => true,
"required" => false,
"user_defined" => false,
"default" => "Main Website",
"searchable" => false,
"filterable" => false,
"comparable" => false,
"visible_on_front" => false,
"unique" => false,
"note" => ""
));
$installer->endSetup();
Model/Categoryoptions.php
<?php
class class Packagename_Modulename_Model_Eav_Entity_Attribute_Source_Categoryoptions extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
/**
* Retrieve all options array
*
* #return array
*/
public function getAllOptions()
{
if (is_null($this->_options)) {
$this->_options = array(
array(
"label" => Mage::helper("eav")->__("Mobile Website"),
"value" => 1
),
array(
"label" => Mage::helper("eav")->__("Main Website"),
"value" => 2
),
);
}
return $this->_options;
}
/**
* Retrieve option array
*
* #return array
*/
public function getOptionArray()
{
$_options = array();
foreach ($this->getAllOptions() as $option) {
$_options[$option["value"]] = $option["label"];
}
return $_options;
}
/**
* Get a text for option value
*
* #param string|integer $value
* #return string
*/
public function getOptionText($value)
{
$options = $this->getAllOptions();
foreach ($options as $option) {
if ($option["value"] == $value) {
return $option["label"];
}
}
return false;
}
/**
* Retrieve Column(s) for Flat
*
* #return array
*/
public function getFlatColums()
{
$columns = array();
$columns[$this->getAttribute()->getAttributeCode()] = array(
"type" => "tinyint(1)",
"unsigned" => false,
"is_null" => true,
"default" => null,
"extra" => null
);
return $columns;
}
/**
* Retrieve Indexes(s) for Flat
*
* #return array
*/
public function getFlatIndexes()
{
$indexes = array();
$index = "IDX_" . strtoupper($this->getAttribute()->getAttributeCode());
$indexes[$index] = array(
"type" => "index",
"fields" => array($this->getAttribute()->getAttributeCode())
);
return $indexes;
}
/**
* Retrieve Select For Flat Attribute update
*
* #param int $store
* #return Varien_Db_Select|null
*/
public function getFlatUpdateSelect($store)
{
return Mage::getResourceModel("eav/entity_attribute")
->getFlatUpdateSelect($this->getAttribute(), $store);
}
}
While Fetching the categories on frontend, filter those by this attribute depending on your website.
I need some validations done, for example, in a date field. I've configured config.sml and config_dev.xml to enable annotations as told in docs. Here is my code:
Entity:
/** *************************************************
* #var date
*
* #Assert\Date(message="Fecha de Nacimiento incorrecta")
* #ORM\Column(name="dfec_nacimiento", type="date", nullable=true)
*/
private $fecNacimiento;
/**
* #param date $fecNacimiento
*/
public function setFecNacimiento($fecNacimiento)
{
$this->fecNacimiento = $fecNacimiento;
}
/**
* #return date
* #Assert\Date()
*/
public function getFecNacimiento()
{
return $this->fecNacimiento;
}
Controller:
[..]
$form = $this->createForm(new IncidenciaType($numSocio,
null,
$listadoTiposIncidencia,
$listadoCentros,
$listadoTiposDoc,
$listadoTiposVia),
$incidencia);
$form->handleRequest($request);//bind del formulario con el request
if ($form->isValid()) {
//Some code
} else {
} else {
$errors = array();
if ($form->count() > 0) {
foreach ($form->all() as $child) {
/**
* #var \Symfony\Component\Form\Form $child
*/
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getErrorMessages($child);
}
}
} else {
/**
* #var \Symfony\Component\Form\FormError $error
*/
foreach ($form->getErrors() as $key => $error) {
$errors[] = $error->getMessage();
}
}
//$errores = array('1' => 'Existen errores en los datos');
return $this->render('Bundle:Environment:registro.html.twig',
array('form' => $form->createView(),
'errores' => $errors));
}
Form:
->add('fecNacimiento','date', array('widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'invalid_message' => 'Debe introducir una fecha válida',
'label' => 'Fecha nacimiento: '))
I'm getting no errors messages when user puts aninvalid date, for example, 121/05/2000 (dd/mm/yyyy format), but $for->isValid() is returning false. But when user puts 77/05/2000, it's working wrong, inserting a date in database adding 77 days to 01/05/2000.
Where I'm wrong?
Thanks in advance
You could try to solve this adding the 'input' option to the options array:
->add('fecNacimiento','date',
array('widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'invalid_message' => 'Debe introducir una fecha válida',
'label' => 'Fecha nacimiento: ',
'input' => 'datetime' ))
I am new on Symfony and I meet the following problem.
I'd like to generate a form without class.
I want to add a Blank() validator on one field.
See below.
class searchPropertyType extends AbstractType
{
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'keywords' => new blank()
));
return array('validation_constraint' => $collectionConstraint);
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('keywords')
->add('neighborhood')
->add('price_min')
->add('price_max')
->add('type')
->add('date_from' , 'date')
->add('date_to' , 'date')
;
}
public function getName()
{
return 'searchProperty';
}
}
The form is properly displayed but still, I can't send the form, I got a HTML5 alert saying that I must fill out this field.
ANy idea? I have been working on that the full day and it drives me crazy.
Thank you so much if you have time to help ;-)
To disable HTML5 client side validation add 'required' => false to getDefaultOptions:
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'keywords' => new blank()
));
return array(
'validation_constraint' => $collectionConstraint,
'required' => false
);
}
public function buildForm(FormBuilder $builder, array $options) {
$builder
->add('neighborhood','text',array('required' => false,))
->add('price_min','text',array('required' => false,))
->add('date_from', 'date', array('widget' => 'single_text', 'format' => 'dd MMM yyyy', 'required' => false))
);
}
Add required=>false
Hope this helps.