I using Phalcon Framework and PostgreSQL
I try to insert an array to database column type: varchar[]:
array(4) { [0]=> string(1) "1" [1]=> string(1) "6" [2]=> string(1) "9" [3]=> string(2) "12" }
But getting following error :
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
Please help me to fix this please
Here is my Model:
<?php
namespace App\Models;
use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness;
class Document extends Model
{
public $id;
public $relatedocument;
public function getSource()
{
return "document";
}
=====Form======
<?php
namespace App\Modules\Backend\Forms;
use Idoc\Models\Document;
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Select;
class DocumentForm extends Form
{
public function initialize($entity = null, $options = null)
{
$data = Document::find();
$this->add(new Select('relatedocument[]', $data, [
'using' => [
'id',
'name'
],
'useEmpty' => true,
'emptyText' => '....',
'multiple' => 'multiple',
'class' => 'form-control search-select'
]));
}
=====addAction======
public function addAction()
{
if ($this->request->isPost()) {
$doc = new Document();
$doc->relatedocument = $this->request->getPost('relatedocument');
if (!$doc->save()) {
$this->flash->error($doc->getMessages());
} else {
$this->flash->success("Văn bản đã được tạo");
Tag::resetInput();
}
}
$this->view->form = new DocumentForm(null);
}
Related
I am using Lumen and I just found an issue. When creating a new model, the code also fills 'updated_at' despite the model is new and it wasn't updated yet (since it was just created). Since this is a crucial flaw and would be strange that it wasn't noticed till now, I presume I am doing something wrong.
App\User.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
const CREATED_AT = 'date_created';
const UPDATED_AT = 'date_updated';
protected $fillable = [
'name',
'email',
'role_id',
'password'
];
protected $hidden = [
'token',
'password',
'date_password_reset',
'token_password_reset'
];
protected $casts = [
'date_created' => 'datetime:Uv',
'date_updated' => 'datetime:Uv',
'date_password_reset' => 'datetime:Uv'
];
protected $with = ['userRoles'];
protected $validationRules = [...];
}
App\Http\Controllers\UsersController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\User;
use Illuminate\Validation\ValidationException;
use Exception;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
...
// first attempt
public function create(Request $request) {
$this->validate($request, (new User)->rules('create'));
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
$user->role_id = $request->input('role_id');
$user->active = $request->boolean('active');
$user->save();
return response()->json(
[
'success' => true,
'message' => 'User successfully created',
'data' => User::query()->find($user->id)
], 200);
}
// second attempt
public function create(Request $request) {
$this->validate($request, (new User)->rules('create'));
$user = new User;
$user->update($request->input());
$user->password = Hash::make($request->input('password'));
$user->active = $request->boolean('active');
$user->save();
return response()->json(
[
'success' => true,
'message' => 'User successfully created',
'data' => User::query()->find($user->id)
], 200);
}
// third attempt
public function create(Request $request) {
$this->validate($request, (new User)->rules('create'));
$user = new User($request->input());
$user->password = Hash::make($request->input('password'));
$user->active = $request->boolean('active');
$user->save();
return response()->json(
[
'success' => true,
'message' => 'User successfully created',
'data' => User::query()->find($user->id)
], 200);
}
}
DB migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Hash;
class CreateUsersTable extends Migration
{
protected $initialUsers = [
[
'name' => 'Administrator',
'email' => 'admin#localhost.local',
'password' => null,
'role_id' => null,
'active' => 1
]
];
protected $initialRoles = [
[
'name' => 'Administrator'
],
[
'name' => 'User'
]
];
protected $initialUserRoles = [];
public function up()
{
DB::beginTransaction();;
Schema::create('users_roles', function (Blueprint $table) {
$table->id();
$table->string('name', 50);
$table->timestamp('date_created')->useCurrent();
$table->timestamp('date_updated')->nullable()->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'));
});
foreach ($this->initialRoles as $data) {
DB::table('user_roles')->insert($data);
}
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->string('email', 255)->unique();
$table->string('password');
$table->unsignedBigInteger('role_id')->nullable();
$table->string('token', 255)->nullable();
$table->tinyInteger('active');
$table->timestamp('date_password_reset')->nullable();
$table->string('token_password_reset')->nullable();
$table->timestamp('date_created')->useCurrent();
$table->timestamp('date_updated')->nullable()->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'));
$table->foreign('role_id')->references('id')->on('users_roles')->onDelete('set null')->onUpdate('cascade');
});
$password = Hash::make('xxx#x');
$role_id = DB::table('users_roles')->where('name', 'Administrator')->value('id');
foreach ($this->initialUsers as $data) {
$data['password'] = $password;
$data['role_id'] = $role_id;
$user_id = DB::table('users')->insertGetId($data);
$this->initialUserRoles[] = [
'user_id' => $user_id,
'role_id' => $role_id
];
}
DB::commit();
}
}
Query created by Eloquent:
array (size=3)
'query' => string 'insert into `users` (`name`, `email`, `password`, `role_id`, `active`, `date_updated`, `date_created`) values (?, ?, ?, ?, ?, ?, ?)' (length=131)
'bindings' =>
array (size=7)
0 => string 'Test User 5' (length=11)
1 => string 'test6#test.net' (length=14)
2 => string '$2y$10$lvRGKuznotd8lqwCj2diIONGjyiAkhaNthWGjQyFWbBqiyuf20wpG' (length=60)
3 => string '1' (length=1)
4 => boolean true
5 => string '2020-06-17 10:30:07' (length=19)
6 => string '2020-06-17 10:30:07' (length=19)
'time' => float 5.5
All three create() attempts are filling up 'updated_at' despite that one should stay NULL until an actual update is done on this model. Can you guys give me any indication what am I doing wrong? Would also like to keep the $model->update($request->input()) functionality if possible, so that I do not need too assign each field manually.
Because Lumen/Laravel is handling the "created_at" and "updated_at" fields on it's own and it sets both to the same timestamp when a record is created (which I do not like), I created my own solution.
First is already in place when creating a migration. I created my own timestamp fields and those are already properly updated by MySQL:
$table->timestamp('date_created')->useCurrent();
$table->timestamp('date_updated')->nullable()->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'));
And of course, disable timestamp handling for Lumen in User.php model:
public $timestamps = false;
But in case that this is not possible for any reason or if you want Lumen/Laravel to handle those, this is the code I wrote in User.php model:
const CREATED_AT = 'date_created';
const UPDATED_AT = 'date_updated';
public $timestamps = false;
public static function boot() {
parent::boot();
static::creating(function ($model) {
$model->{self::CREATED_AT} = $model->freshTimestamp();
});
static::updating(function ($model) {
$model->{self::UPDATED_AT} = $model->freshTimestamp();
});
}
I added the following to a Sonata admin in order to filter by category. However, the list does not show NULL as an option for category. I want also want to be able to filter by category for when category is NULL instead of an entity.
How can one achieve this? My current configuration:
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add("category");
}
Try this:
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add("category", 'doctrine_orm_callback', array(
'callback' => function ($queryBuilder, $alias, $field, $value) {
/**
* #var QueryBuilder $queryBuilder
*/
if ($value['value']) {
if ($value['value'] == 0) {
$queryBuilder->andWhere($queryBuilder->expr()->isNull($alias.'.category'));
return true;
} else {
$category = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager')->getReference('AcmeBundle:Category', $value['value']);
$queryBuilder->andWhere($queryBuilder->expr()->eq($alias.'.category', $category));
return true;
}
}
},
'field_type' => 'choice',
'field_options' => array(
'choices' => $this->getCategoryChoices()
),
'label' => 'Category'
));
}
private function getCategoryChoices()
{
$categories = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager')->getRepository('AcmeBundle:Category')->findAll();
$choices["0"] = "NULL";
foreach($categories as $category) {
$choices["{$category->getId()}"] = $category->getName();
}
return $choices;
}
I'm having hard time with a weird behaviour of fileinput.
This is my form:
namespace Frontend\Form;
use NW\Form\Form;
use Zend\InputFilter;
use Zend\Form\Element;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
class EnrollStructure extends Form implements ServiceManagerAwareInterface
{
protected $sm;
public function __construct($name=null) {
parent::__construct("frmEnrollStructure");
$this->setAttribute("action", "/registrazione_struttura/submit")
->setAttribute('method', 'post')
->setAttribute("id", "iscrizione_struttura")
->setAttribute("class", "form fullpage");
$this->addInputFilter();
}
public function init()
{
$structureFs = $this->sm->get('Structure\Form\Fieldsets\Structure');
$structureFs->setUseAsBaseFieldset(true);
$structureFs->remove("id")
->remove("creationTime")
->remove("latLon");
$file = new Element\File("images");
$file->setAttribute('multiple', true);
$this->add($structureFs)->add($file);
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Iscriviti',
'id' => 'sbmtEnrollStructure',
'class' => 'submit_btn'
),
));
$this->setValidationGroup(
array(
'structure' =>
array(
'companyname',
'vatNumber',
'addressStreet',
'addressZip',
'addressCity',
'addressRegion',
'fax',
'publicPhone',
'publicEmail',
'website',
'status',
'ownerNotes',
'category',
'subcategory',
"facilities",
"agreeOnPolicy",
"agreeOnPrivacy",
"subscribeNewsletter",
"contact" => array("name", "surname", "email", "role", "phone"),
),
"images"
));
}
/**
* Set service manager
*
* #param ServiceManager $serviceManager
*/
public function setServiceManager(ServiceManager $serviceManager)
{
$this->sm = $serviceManager;
}
public function addInputFilter()
{
$inputFilter = new InputFilter\InputFilter();
// File Input
$fileInput = new InputFilter\FileInput('images');
$fileInput->setRequired(true);
$fileInput->getValidatorChain()
->attachByName('filesize', array('max' => "2MB"))
->attachByName('filemimetype', array('mimeType' => 'image/png,image/x-png,image/jpg,image/jpeg'))
->attachByName('fileimagesize', array('maxWidth' => 2048, 'maxHeight' => 2048));
$inputFilter->add($fileInput);
$this->setInputFilter($inputFilter);
}
}
Basically, I mainly use a fieldset which contains most of the data I request to the user, plus a File input field.
This is the Fieldset Structure: (most important parts..)
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Validator\Identical;
use Zend\Validator\NotEmpty;
use Zend\Validator\Regex;
use Zend\Validator\StringLength;
class Structure extends Fieldset implements InputFilterProviderInterface, ServiceManagerAwareInterface
{
protected $sm;
public function __construct()
{
parent::__construct('structure');
}
public function init()
{
$this->setHydrator(new DoctrineHydrator($this->_entityManager(),'Structure\Entity\Structure'));
$this->setObject($this->sm->getServiceLocator()->get("Structure_Structure"));
$id = new Element\Hidden("id");
$name = new Element\Text("companyname");
$name->setLabel("Ragione Sociale");
...........
}
public function getInputFilterSpecification()
{
return array
(
"id" => array(
"required" => false,
),
"companyname" => array(
"required" => true,
"validators" => array(
array('name' => "NotEmpty", 'options' => array("messages" => array( NotEmpty::IS_EMPTY => "Inserire la ragione sociale")))
),
),
.....
}
}
This is my controller:
public function submitAction()
{
try {
$this->layout("layout/json");
$form = $this->getForm('Frontend\Form\EnrollStructure');
//$form->addInputFilter();
$structure = $this->getServiceLocator()->get("Structure_Structure");
$viewModel = new ViewModel();
$request = $this->getRequest();
if ($request->isPost())
{
$post = array_merge_recursive
(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($post);
if ($form->isValid())
{
$structure = $form->getObject();
$contact = $structure->getContact();
$this->getServiceLocator()->get('Structure_ContactService')->save($contact);
$files = $request->getFiles()->toArray();
if(isset($files['images']))
{
$count = 3;
foreach($files['images'] as $pos => $file)
{
$fpath = $this->getServiceLocator()->get('RdnUpload\Container')->upload($file);
if(!empty($fpath))
{
if(--$count ==0) break;
$asset = $this->getServiceLocator()->get("Application_AssetService")->fromDisk($fpath, $file['name']);
$this->getServiceLocator()->get("Application_AssetService")->save($asset);
$structure->addImage($asset);
}
}
}
$this->getServiceLocator()->get('Structure_StructureService')->save($structure);
$retCode = RetCode::success(array("iscrizione_struttura!" => array("form_submit_successfull")), true);
}
else
{
$messages = $form->getMessages();
if(empty($messages))
$retCode = RetCode::error(array("iscrizione_struttura" => array("need_at_least_one_file" => "missing file")), true);
else
$retCode = RetCode::error(array("iscrizione_struttura" => $messages), true);
}
$viewModel->setVariable("retcode", $retCode);
return $viewModel;
}
} catch(Exception $e)
{
throw $e;
}
}
The strange thing is that if i remove from the field "images" the "multiple" attribute everything works fine, causing the form not to validate and i get this message:
[images] => Array
(
[fileUploadFileErrorFileNotFound] => File was not found
)
While, if i set the attribute multiple, and the user does not upload a file i get no error, but the form gets invalidated (this is the reason for this "bad" code in my controller:)
$messages = $form->getMessages();
if(empty($messages))
$retCode = RetCode::error(array("iscrizione_struttura" => array("need_at_least_one_file" => "missing file")), true);
else
$retCode = RetCode::error(array("iscrizione_struttura" => $messages), true);
I found the problem was caused by the Jquery form plugin, without it it works fine. :( In case somebody needs, I think the correct action code can be found here (I haven't tryied it anyway)
https://github.com/cgmartin/ZF2FileUploadExamples/blob/master/src/ZF2FileUploadExamples/Controller/ProgressExamples.php
Hi I'm trying to implement the method fetchAll like the Album example but It doesn't work.
When I try to print the result with a var_dump I get this
object(Zend\Db\ResultSet\ResultSet)#258 (8) {
["allowedReturnTypes":protected]=>
array(2) {
[0]=>
string(11) "arrayobject"
[1]=>
string(5) "array"
}
["arrayObjectPrototype":protected]=>
object(Application\Model\Ubigeo)#242 (5) {
["codDpto"]=>
NULL
["codProv"]=>
NULL
["codDist"]=>
NULL
["name"]=>
NULL
["idUbigeo"]=>
NULL
}
["returnType":protected]=>
string(11) "arrayobject"
["buffer":protected]=>
NULL
["count":protected]=>
int(2057)
["dataSource":protected]=>
object(Zend\Db\Adapter\Driver\Pdo\Result)#257 (8) {
["statementMode":protected]=>
string(7) "forward"
["resource":protected]=>
object(PDOStatement)#248 (1) {
["queryString"]=>
string(52) "SELECT `ubigeo`.* FROM `ubigeo` ORDER BY `name` DESC"
}
["options":protected]=>
NULL
["currentComplete":protected]=>
bool(false)
["currentData":protected]=>
NULL
["position":protected]=>
int(-1)
["generatedValue":protected]=>
string(1) "0"
["rowCount":protected]=>
int(2057)
}
["fieldCount":protected]=>
int(5)
["position":protected]=>
int(0)
}
This is my serviceConfig:
public function getServiceConfig() {
return array(
'factories' => array(
'Application\Model\UbigeoTable' => function ($sm) {
$tableGateway = $sm->get('UbigeoTableGateway');
$table = new UbigeoTable($tableGateway);
return $table;
},
'UbigeoTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Ubigeo());
return new TableGateway('ubigeo', $dbAdapter, null, $resultSetPrototype);
}
),
);
}
Any help would be appreciate
According to var_dump your table contains these rows : codDpto, codProv, codDist, name and idUbigeo.
You can access the results this way :
foreach($resultSet as $row)
{
$result = '<p>codDpto: '.$row['codDpto'];
$result .= ', codProv: '.$row['codProv'];
$result .= ', codDist: '.$row['codDist'];
$result .= ', name: '.$row['name'];
$result .= ', idUbigeo: '.$row['idUbigeo'].'</p>';
echo $result;
}
I'm trying to figure out what's wrong with my first tutorial using Zend Skeleton App. I'm using Zend Studio 10 + ZendServer and Zf2.2; managed to get the skeleton app working and now got stuck on a missing class problem (see error below). I have tried various approaches but the result is the same: it's not working. Here are my files, any help would be appreciated.
My error:
Fatal error: Class 'Album\Model\AlbumTable' not found in C:\Program
Files\Zend\Apache2\htdocs\zf2album\module\Album\Module.php on line 55
Album/Module.php
namespace Album;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
class Module implements ServiceProviderInterface {
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
// if we're in a namespace deeper than one level we need to fix the \ in the path
__NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
the AlbumController.php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController; use
Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController { protected
$albumTable;
public function indexAction()
{
return new ViewModel(array(
'albums' => $this->getAlbumTable()->fetchAll(),
));
}
public function addAction()
{
}
public function editAction()
{
}
public function deleteAction()
{
}
public function fooAction()
{
// This shows the :controller and :action parameters in default route
// are working when you browse to /album/album/foo
return array();
}
public function getAlbumTable()
{
if (!$this->albumTable) {
$sm = $this->getServiceLocator();
$this->albumTable = $sm->get('Album\Model\AlbumTable');
}
return $this->albumTable;
} }
AlbumModel.php
namespace Album\Model;
use Zend\Db\TableGateway\TableGateway;
class AlbumTable {
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getAlbum($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveAlbum(Album $album)
{
$data = array(
'artist' => $album->artist,
'title' => $album->title,
);
$id = (int)$album->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getAlbum($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Form id does not exist');
}
}
}
public function deleteAlbum($id)
{
$this->tableGateway->delete(array('id' => $id));
} }
Assuming this isn't a typo in your question, the filename for the class AlbumTable should be AlbumTable.php, not AlbumModel.php.