I've found a technique to insert data in two entities with one form here:
http://forum.symfony-project.org/viewtopic.php?f=23&t=42011
My problem is that getData() return an array of objects and I can't use it with the persist() method because it expects an object. How can I fix it?
You might be looking for something like this:
$data = $form->getData();
foreach ($data as $object) {
$em->persist($object);
}
$em->flush();
Related
I have TYPO3 extension with some custom function in repository. I have in this model 1:n relation (events is parent and dates are child elements).
I tried to get the beginning date of child elements with
foreach($events as $key => $value) {
echo $value->getDates()->getBeginn();
}
But I get error "Call to undefined method TYPO3\CMS\Extbase\Persistence\ObjectStorage::getBeginn()". How can I initalize the ObjectStorage in repository?
Thanks
Martin
If I understand you correctly, getBeginn is a function in the Date model and each Event object can have multiple Date objects attached to it.
Assuming this is correct, the getDates function in an Event object will return a collection of Date objects, not just one. In TYPO3 this is done using an ObjectStorage. You can see (and use) this as an array which (in this case) contains Date objects.
So for example you can do:
foreach($events as $event) {
foreach ($event->getDates() as $date) {
echo date->getBeginn();
}
}
I'm using the "processDatamap_afterDatabaseOperations" hook within my extension to transfer content from a newly created News (tx_news_domain_model_news) to an API.
TYPO3 Version is 6.2.11 and if I var_dump or trying to access the category using $record->getCategories() it's empty. Same with related files, falmedia works. Here is my code:
public function processDatamap_afterDatabaseOperations($status, $table, $id, array $fieldArray, \TYPO3\CMS\Core\DataHandling\DataHandler &$pObj) {
if ($table == 'tx_news_domain_model_news' && $status == 'new') {
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
$news = $objectManager->get('GeorgRinger\News\Domain\Repository\NewsRepository');
$record = $news->findByUid($pObj->substNEWwithIDs[$id]);
Hope anybody knows what I'm doing wrong here. I've been trying this like forever and don't get it.
Thanks in advance for your help.
That's likely because "afterDatabaseOperations" is called for each record insertion/update in each table, and that the relation between the record and the categories has not been established yet.
Only after all insertions/updates have been done, the method processRemapStack is called by the DataHandler, that sets/fixes all the relations between the various records (e.g. wherever there was a "NEW.." relation in the datamap, the correct uids are set).
The only hook you can use, where alle records have the correct relations is the processDatamap_afterAllOperations hook, that you can find at the very end of the process_datamap in the DataHandler class.
That one only takes a single argument though (the DataHandler instance), so you probably have to try and get the inserted news records using the "datamap" property of the DataHandler reference.
I am new to Codeigniter and have two small question
1: How can I pass two table data array to view?
$data['customers']=$this->customer_model->get_all_customers();
//$products['product']=$this->customer_model->get_all_categories();
//$this->load->view("customer_view",$data);
$product['cats']=$this->customer_model->get_all_categories();
$this->loadViews("customer_view", $this->global, $data, $product);
But I am getting error
2: I am joining two tables to get two tables data but I am getting only one table data?
public function get_all_categories()
{
/*// Get Data from Two tables
$this->db->select('*');
$this->db->from('category');
$this->db->join('customers','customers.categoryID=category.categoryID','Inner');
$query=$this->db->get();
please help me to resolve.
This :
$query=$this->db->get();
Should be :
$query=$this->db->get()->result_array();
Here is solution for Passing multi variable to view
Controller
$data['customers']=$this->customer_model->get_all_customers();
$data['cats']=$this->customer_model->get_all_categories();
$data ['customers'] = $customers;
$data ['cats'] = $categories;
$this->loadViews("customer_view", $this->global, $data);
In view we will use $customers and $categories
I'm looking for best way of getting form element values inside isValid() method.
I had something like this isValid():
public function isValid($data) {
$start = (int)($data['start_hour'] . $data['start_minute']);
$end = (int)($data['end_hour'] . $data['end_minute']);
if ($start >= $end) {
$this->getElement('start_hour')->addError('Start time should be less than end time');
return false;
}
return parent::isValid($data);
}
but problem is that when my data structure changes I have to change validation too.
For example, now values of start_hour, start_minute, etc becomes elements of multidimensional array, and I need edit validation like
public function isValid($data) {
$start = (int)($data['send']['start_hour'] . $data['send']['start_minute']);
$end = (int)($data['send']['end_hour'] . $data['send']['end_minute']);
.......
}
It would be great to get value of element by permanent key (like element name), so my isValid could looks like:
public function isValid($data) {
$start = (int)($this->getElement('start_hour')->getValue() . $this->getElement('start_minute')->getValue());
$end = (int)($this->getElement('end_hour')->getValue() . $this->getElement('end_minute')->getValue());
.......
}
but $this->getElement('start_hour')->getValue() inside validation method return an empty value.
Is this possible to get element value in such way?
Thanks.
Try with $this->getValue('start_hour');
If this code takes place in the isValid() method of the form, then sure you could do it as you have described. It's just that isValid() usually needs some data passed - $form->isValid($_POST), for example - and you just end up ignoring it (assuming the parent is Zend_Form, which has an empty isValid() method; intermediate ancestors could potentially inspect the passed data). I would consider that to be potentially confusing.
Al alternative could be to create a custom validator, attach it to one of the form elements (say, the start_hour elements). The signature for the validator's isValid() can use the optional $context parameter - isValid($value, $context = null). When you call $form->isValid($data), it will pass that $data as the $context to the the element validator. You can then use that $context variable to inspect the other values (start_min, end_hour, end_min, etc).
Try calling
$form->populate($data)
Before calling isValid that way the data will be in your form.
Then $this->getValue('start_hour'); should work from within isValid().
So to be sure:
Somewhere in your code (probably controller) there is somthing like:
$this->view->form = new MyForm();
$this->populate($data); //add this
if($this->view->form->isValid($data)){
//do stuff
}
For example:
I need to retrieve a set of male User's IDs, First Names, and Last Names, but nothing else.
So I have a function in UserMapper called fetchAllMaleUsers() that returns a set of User entities.
i.e:
public function fetchAllMaleUsers() {
$select = $this->getDbTable()
->select()
->from($this->getDbTable(),
array('ID', 'FirstName', 'LastName'))
->where('Gender = ?', 'M');
$resultSet = $this->getDbTable()->fetchAll($select);
$users = array();
foreach ($resultSet as $row) {
$user = new Application_Model_User();
$user->setId($row->ID)
->setFirstName($row->FirstName)
->setLastName($row->LastName);
$users[] = $user;
}
return $users;
}
Does this function belong in the mapper layer?
Is it ok to only set the Id, Firstname, and LastName of each User entity?
1) Perfectly fine for the mapper/Gateway or however you call it.
2) You can do but i highly disencourage this. Why? Later on in your application you can't tell from where you did get the model. So you'd have to check if a value is set in your model each time you're not sure if it is set or you'd need some autoloading stuff for missing values (which is as worse as missing stuff). Another reason is that you can't reuse the function for other porposes where you might need other properties of the user model. Last reason afaik is, that a model represents the complete entity at any time, not parts of it. And there's no real reason why not to load all fields (beside references to other entities why should be autoloaded anyways).
Along with Fge's reply I believe that $resultSet should already be returning values of type Application_Model_User. If not, you may need to set $_rowClass in Application_Model_DbTable_User.