I'm newbie with TYPO3.
I'm facing with this issue, can not get data from database.
I have a plugin with this configure
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
'Locations.' . $_EXTKEY,
'Locationsfe',
array(
'Location' => 'list, show, showprice, listprice, listcategory, scan,sharedoffice',
'Category' => 'list, show',
'Pricing' => 'list, show, showprice, listprice, scan, test',
'Template' => 'list, show',
),
// non-cacheable actions
array(
'Location' => '',
'Category' => '',
'Pricing' => 'test',
'Template' => '',
)
);
And in my controller I have this function
public function testAction() {
// Get current language
$currentLanguage = $GLOBALS['TSFE']->sys_language_uid;
$pricing = $this->pricingRepository->findAll();
print_r($pricing);
die('Passed');
}
I also added this line to Constants
plugin.tx_locations.persistence.storagePid = 164
I also created a typo Script
plugin.tx_locations {
view {
templateRootPath = {$plugin.tx_locations.view.templateRootPath}
partialRootPath = {$plugin.tx_locations.view.partialRootPath}
layoutRootPath = {$plugin.tx_locations.view.layoutRootPath}
}
persistence {
storagePid = 164
}
features {
# uncomment the following line to enable the new Property Mapper.
# rewrittenPropertyMapper = 1
}
}
But all of above does not work. Just white page return.
I also read this
extbase repository findAll() returns result null
So, what happen? I don't know why. Can you help me to figure it out please.
Thanks in advance.
Ok i suppose you have created some records from model type pricing in your backend right ;).
Then please try this in your controller:
$this->pricingRepository->setDefaultQuerySettings($this->pricingRepository->createQuery()->getQuerySettings()->setRespectStoragePage(false)); /* here we ignore the storage pid to be sure that we look in the entire system, if this works we have some hints that somthing with the storage pid is wrong */
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->pricingRepository); /*please use this for debugging is much easier with this :D*/
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->pricingRepository->findAll());
can you post the debugs please.
sorry i can't comment i don't have the right reputation :(
Related
How do you set a default value for a field in a model?
EDIT:
I have tried the method using _schema as suggested, but the default value is not being used.
public $_schema = array(
'newsletter' => array(
'default' => 1
),
);
you should always try to set default values from the controller:
http://www.dereuromark.de/tag/default-values/
It would be better to set the default value in the database? I don't really see why you would want to do it CakePHP side...
As the above suggestions do not work for me, so I have found my own. The answer is very similar to the written above, but with one little correction. (works with CakePHP 2.6.1)
The default value can be set in the controller in add function ("request" is needed).
$this->request->data['Country']['hasFlag'] = 1;
Full code example:
public function add() {
if ($this->request->is('post')) {
$this->Country->create();
if ($this->Country->save($this->request->data)) {
...
} else {
...
}
}
$this->request->data['Country']['hasFlag'] = 1; // default value passing to the view
}
Some philosophy:
1) Why this is needed - If we have a boolean attribute in the database, the newly created object in Cakephp does not take into the account default values from database. And if we leave checkbox unchecked in the Add form of the new object and submit it to the database - it means this attribute value is false (not value not set)
2) Is this an ideal place to set default value? - No, this is not an ideal place, as all information about the Object and its data must be in the Model, but I haven't managed to assign default value in the model. Even using _schema variable or create function.
You can set the value in database and get it managed in schema, e.g.:
public $items = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 10, 'unsigned' => false, 'key' => 'primary'),
'quantity' => array('type' => 'decimal', 'null' => false, 'default' => '1.00', 'length' => '12,2', 'unsigned' => false),
// ^^^^^^^^^^^^^^^^^^^
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_spanish_ci', 'engine' => 'InnoDB')
);
This default value can be read later in model or controller though the model's schema property:
// Controller example
$itemSchema = $this->Item->schema();
$defaultQuantity = $itemSchema['quantity']['default'];
// ... or:
$quantityInfo = $this->Item->schema('quantity');
$defaultQuantity = $quantityInfo['default'];
In recent PHP versions it can be a one-liner:
$defaultQuantity = $this->Item->schema('quantity')['default'];
This works in Cake/2.5 with MySQL adapter (no idea of other scenarios).
databases get big, so you cannot remember all those defaults you've set. Let's keep it simple:
You set default values in the controller (it's easy to read the code, if default values for certain actions are set at the beginning as class properties).
For example:
class UsersController extends AppController {
private $registerDefaults = array(
'group_id' => '1'
);
public function register() {
if ($this->request->is('post')) {
/*
* This is where you set default value
* Here's what I do for default group that user should be assigned to
*/
$this->request->data['User']['group_id'] = $this->registerDefaults['group_id'];
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('You have been successfully registered.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('We're unable register this user.'));
}
}
}
You can't always remember default values set in the database if you've got about 60-80 tables with complicated relations.
AND
My advice is that you don't set defaults that depend on your current settings, be more flexible: create configuration table or set defaults in AppController in order to find it wth a blink of an eye.
I am having issues getting CakeDC's tag's plugin to work. I have read the documentation very carefully, but it seems the docs are very old.
// Totally works. Does what it is supposed to do, does not
// complain of missing models.
$tag = $this->Upload->Tagged->find('tagged',
array('by' => $tagname, 'model' => 'Upload', 'conditions' =>
array( 'Upload.soft_delete !=' => 1) ));
// 100% correct according to the 3 year old documentation.
// Complains of a missing "taggeds" model.
// Table taggeds for model Tagged was not found in datasource default.
// Undefined index: tagged [CORE/Cake/Model/Model.php, line 2731]
$this->paginate['Tagged'] = array(
'model' => 'Upload',
'tagged',
'by' => $tagname);
$tag = $this->paginate('Tagged');
I read the documentation here at: https://github.com/CakeDC/tags/wiki/Find-tagged-objects
At first, I experienced the Indirect modification of overloaded property $paginate ... no effect" bug until I added public $paginate = array(); to that the top of my controller. This has not helped the other error.
Hopefully I am missing something simple here.
UPDATE: I changed the code to look like this
$this->Paginator->settings['Tagged'] = array(
'tagged',
'model' => 'Upload',
'by' => $tagname
);
$this->Paginator->paginate('Tagged');
and I get this error: Error: Call to a member function paginate() on a non-object
I eventually made it work adding in the top of Controller
public $components = array('Paginator');
Then in my method
$this->Paginator->settings['Tagged'] = array(
'tagged',
'model' => 'Upload',
'by' => $tagname
);
$this->Paginator->paginate('Tagged');
I experienced the Indirect modification of overloaded property
$paginate ... no effect" bug
That's your issue and not really a bug. CakePHP has changed a little, try this:
$this->Paginator->settings['Tagged'] = array(
'tagged',
'model' => 'Upload',
'by' => $tagname
);
$this->Paginator->paginate('Tagged');
You're welcome to improve the documentation. We're maintaining currently 14 plugins for free, any kind of help is appreciated. Give something back and help improving the docs. :)
removeOriginal Dont work on MeioUpload !
i have this code in my post model :
/model/post.php
public $actsAs = array(
'MeioUpload.MeioUpload' => array(
'avatar' =>array(
'thumbnails' => true ,
'thumbsizes' => array('small' => array('width'=>100, 'height'=>100)),
'thumbnailQuality' => 75,
'thumbnailDir' => 'thumb',
'removeOriginal' => true
)
)
);
i want to upload thumbs only , I do not need the source picture .
(cakephp 2.1.2)
thanks
This Behaviour is depreciated and no longer supported (https://github.com/jrbasso/MeioUpload) but I had the same problem and have no need to migrate yet.
For anyone in the same boat as me, you can fix it as follows:
Open Model/Behavior/MeioUploadBehavior.php
Look for this block of code which appears TWICE:
// If the file is an image, try to make the thumbnails
if ((count($options['thumbsizes']) > 0) && count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) {
$this->_createThumbnails($model, $data, $fieldName, $saveAs, $ext, $options);
}
After the SECOND occurrence insert the following code (which does appear already under the first occurence):
if ($options['removeOriginal']) {
$this->_removeOriginal($saveAs);
}
I did this in my project and seems to be working just fine so far!
i got a problem with designing my Database and the CakePHP code around it.
I have 4 Models,
ServerName
VirtualMachine
ServerHousing
ManagedServer
In ServerName, i want to save all ServerNames which then can be used in either one of the three other models. How can i achieve that i am only able to link one ServerName to either one of the other models?
Thank you guys in advance.
EDIT:
I now did it a little bit different. First of all it need to be done in the Model itself.
I used the validate option in cakePHP's models.
The code is like this:
public $validate = array(
'server_name_id' => array(
'rule' => 'serverNameTaken',
'message' => 'This Servername has already been taken.'
)
);
public function serverNameTaken()
{
$this->ManagedServer = ClassRegistry::init("ManagedServer");
// Assuming the server_name_id was passed from the form...
$server_name_id = $this->data['VirtualMachine']['server_name_id'];
// Check if this servername_id is already saved in virtual_machines
if ($this->ManagedServer->find('count', array(
'conditions' => array(
'ManagedServer.server_name_id' => $server_name_id
)
)) > 0
) {
// Found the server_name in the VirtualMachine model!
return false; // Prohibit saving the data
}
if ($this->find('count', array(
'conditions' => array(
'VirtualMachine.server_name_id' => $server_name_id
)
)) > 0
) {
// Found the server_name in the VirtualMachine model!
return false; // Prohibit saving the data
}
// Do this for the other models too. If a return false is not hit by now,
// everything should be fine and you can...
return true;
}
Same code I used in the other models, only the code had to be altered.
Thanks again for you answer!!!
You can't do this on the Model layer. Models are either associated or they are not. What you are probably looking for is some logic in your Controller that checks if a row for ServerName X has already been saved in any of the other models, prior to saving it from another model.
The beforeSave function is useful for this. For example, in your Controller, put this:
public $uses = array('VirtualMachine', 'ServerHousing', 'ManagedServer');
public function beforeSave() {
// Assuming the servername_id was passed from the form...
$servername_id = $this->request->data['servername_id'];
// Check if this servername_id is already saved in virtual_machines
if($this->VirtualMachine->find('count', array(
'conditions' => array(
'VirtualMachine.servername_id' => $servername_id
)
)) > 0) {
// Found the servername in the VirtualMachine model!
return false; // Prohibit saving the data
}
// Do this for the other models too. If a return false is not hit by now,
// everything should be fine and you can...
return true;
}
Hope this will get you going in the right direction.
I am setting up a user/group system that allows users to send requests to join a group.
I can't seem to load the associated model and add a row. It's really really really difficult to resist the urge to just use $this->query() and be done with it... but I'm trying to learn the Cake conventions and do things the right way.
In my group model's function for handling group join requests:
$this->loadModel('GroupRequest');
$this->data = array('GroupRequest' =>
array('user_id' => $uid, 'group_id' => $gid));
if($this->GroupRequest->save($this->data)){ echo 'save success'; }
else { echo 'save fail'; }
Here are the errors I get when I run this:
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'loadModel' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: loadModel
Notice (8): Undefined property: Group::$GroupRequest [APP/models/group.php, line 124]
Fatal error: Call to a member function save() on a non-object in /home/wxworksmat/sspot3/app/models/group.php on line 124
I also tried using App::import:
App::import('Model','GroupRequest');
I don't get any SQL errors importing the Model this way, but it still doesn't work. I get the following error on the save() or create() call:
Fatal error: Call to a member function save() on a non-object in /home/wxworksmat/sspot3/app/models/group.php on line 124
You are confusing controller and model methods
$this->loadModel()
is a controller method and can only be used there.
You should always use
$this->ModelName = ClassRegistry::init('ModelName');
everywhere else
I might be wrong, and please excuse me if I'm wrong, but it looks like you don't understand the concept of the framework very well. It is difficult to answer your question without giving you a complete tutorial.
This said, everything relies on model associations. If it's done correctly, things are getting easy. You should read:
Associations: Linking Models Together
Once you have your models correctly linked, you will be able to save the primary model, as well as the related model, very easily.
Saving Related Model Data (hasOne, hasMany, belongsTo)
As I understand, you are trying to do this from inside a model?
class GroupRequest extends AppModel {
public function associate($user, $group) {
$data["GroupRequest"] = array("user_id" => $user, "group_id" => $group);
$this->save($data);
}
}
Then in your Controller (assuming group_requests_controller)
$this->GroupRequest->associate($user, $group);
If you're calling this from another controller you would loadModel first
$this->loadModel("GroupRequests");
$this->GroupRequest->associate($user, $group);
However, if you're doing all of this from within GroupRequests controller you should be able to save directly, without making a separate method for it
public function add() {
$this->GroupRequest->create();
$this->GroupRequest->save($this->data); #for < 2.0
}
Your view should be something like
<?php
echo $this->Form->create("GroupRequest");
echo $this->Form->input("user_id");
echo $this->Form->input("group_id");
echo $this->Form->end("Submit");
?>
The problem I had was that I didn't have the correct model association declarations at the top of my model.
Now I have:
group.php
var $hasMany = 'GroupRequest';
group_request.php
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public function new_request($user, $group) {
$data["GroupRequest"] = array("user_id" => $user, "group_id" => $group, 'status' => 'pending');
if($this->save($data)){ return true;} else {return false;}
}
Now because everything is set up CORRECTLY... I can do this in my group.php model:
$this->GroupRequest->new_request($uid,$gid)
As an added bonus, because the assocations are populating properly, when I do $this->find in my group or user model, now all the related GroupRequest entries show up. Bonus data FTW.