I'm looking for a hook that let's me modify the database output, when editing my extension in the backend, before it's printed to the TCE fields.
I tried getSingleField_preProcess in class.t3lib_tceforms.php but that did not contain any relevant data to my extension.
getSingleField_preProcess should be the correct hook for what you want. I think the problem is that your function is called for records of every table, not just yours. You have to differentiate when to do any processing based on the table that is being rendered. The name of the table is passed to your getSingleField_preProcess() method.
Your ext_localconf.php should register your hook:
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tceforms.php']['getSingleFieldClass'][] = 'tx_yourextension_be';
...where tx_yourextension_be is the name of your class designated for the backend processing. This class then must contain getSingleField_preProcess() method:
public function getSingleField_preProcess($table, $field, &$row, $altName, $palette, $extra, $pal, &$pObj) {
// ...processing...
}
As you see, several variables are passed to your method. $table contains the name of the table to which the processed record belongs to. $field is the name of the field that is being rendered. $row contains the whole record that you can manipulate.
Probably you want to use TCEmain hook
function processDatamap_preProcessFieldArray(array &$incomingFieldArray, $table, $id, t3lib_TCEmain &$reference) {
if ($table == 'tx_yourext_table') {
$a = $incomingFieldArray['field_a'];
$b = $incomingFieldArray['field_b'];
$incomingFieldArray['field_c'] = $a . ' ' . $b;
}
}
OR/AND
function processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, &$reference) {
if ($table == 'tx_yourext_table') {
if ($status == 'update') {
$this->doSomethingWithRecordAfterUpdate($id);
}
}
}
Of course you need to register the hook in ext_localconf.php of your extension, for an example:
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass']['yourext']
= 'EXT:yourext/class.tx_yourext_tcemain.php:tx_yourext_tcemain';
Related
I'm trying to use create() method to create an object say 'user' from laravel-excel Import class say 'UserImport'. In the collection method, I grabbed the first row as the properties of the user and every cell in the subsequent row bears the data for the user on each row.
Using the create method will ensure that field that is not in my fillable will not be inserted as fields are dynamically gotten. I need the create method to use setGenderAttribute defined on the User model so as to transform 'male', 'm' in the excel gender column to constant User::GENDER_MALE and 'female', 'f' to constant User::GENDER_FEMALE.
public function collection(Collection $rows)
{
$keys = [];
// Extract spreadsheet head
foreach ($rows[0] as $key) {
$keys[] = Str::snake($key);
}
$j = 0;
foreach ($rows as $row) {
// Offset the head from the data set
if ($j == 0) {
$j++;
continue;
}
// get data from each row
$data = [];
$i = 0;
foreach ($keys as $key) {
$data[$key] = $row[$i];
$i++;
}
// Create user from each row
$user = User::create($data);
event(new MemberAdded($user));
}
}
It throws the following error integer value: 'male' for column users.gender
I've gotten to the answer. Laravel create method on eloquent actually call all the set{field}Attribute before actually doing database insertion. The problem was in one of the 'set' method. There was a bug in it and laravel fall back to the original field which was a string whose column was defined as integer in the database table schema.
Basically what i want to do is write this query with cakephp 3 query builder :
SELECT * FROM question as q innerjoin answers as a where q.question = $question AND a.id NOT IN = $avoidedIDs
codes for table class
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use App\Model\Entity\Comlib;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Validator;
use Cake\ORM\TableRegistry;
class ComlibsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('questions');
// join the tables
$this->hasMany('Answers' , [
'foreignKey' => 'question_id'
]);
}
public function LFA( $live_req) {
$query = $this->find()->where(['question' => $live_req])->contain(['Answers'])->LIMIT(6);
$query = $query->toArray();
//include 5 answer
return $query;
}
public function LFM($theid , $avoidedIDs, $question )
{
$query = $this->find()->where(['question' => $question])->contain(['Answers' => function($q){
return $q
->where(['Answers.id NOT IN' => $avoidedIDs] );
}
]);
$query = $query->toArray();
debug($query);
return $query;
}
}
the error that i get is : Impossible to generate condition with empty list of values for field (Answers.id).
but when i print_r($avoidedIDs) i get the values that i passed so im sure that $avoidedIDs is not empty , at least not out of contain function and thats what makes it more complicated for me, but when i put ONE number instead of my variable it will execute , if i put 1,2,3,4 still it will execute only the first one!
what am i doing WRONG for the past 2 days ?????
tnx for any help
It is because you are trying to use $avoidedIDs in an anonymous function (Closure) call, which is not available there.
You should make it available for the function.
->contain(['Answers' => function($q) use ($avoidedIDs){..}
Closures may also inherit variables from the parent scope. Any such
variables must be passed to the use language construct.
http://www.php.net/manual/en/functions.anonymous.php
Question: What is the best way to track a fields data source?
I have been going through numerous files and all the references that I can find are correct.
The issue is that the "Last Name" field is showing the contacts full name.
Any ideas would be greatly appreciated.
I am working with SugarCRM CE 6.5.13
Thanks.
Fields are defined in modules/SomeModules/vardefs.php or if it is a
custom_field in the db table fields_meta_data
in the modules bean modules/SomeModules/SomeModule.php you can overwrite default behavior to generate for example a full_name like this:
For detail view:
function fill_in_additional_detail_fields() {
parent::fill_in_additional_detail_fields();
$this->full_name = 'Something else';
}
For list view:
function fill_in_additional_list_fields() {
parent::fill_in_additional_list_fields();
$this->full_name = 'Something else';
}
Or by overriding the standard function _create_proper_name_field:
/**
* This function helps generate the name and full_name member field
* variables from the salutation, title, first_name and last_name fields.
* It takes into account the locale format settings as well as ACL settings
* if supported.
*/
public function _create_proper_name_field() {
parent::_create_proper_name_field();
$full_name = trim(trim($this->first_name) . ' ' . trim($this->last_name));
if (!empty($full_name) && !empty($this->name)) {
$full_name .= ' - ';
}
if (!empty($this->name)) {
$full_name .= $this->name;
}
$this->full_name = $full_name;
}
does this code gives me the last inserted id of the record, even on a heavy load page?
db = Zend_Db_Table::getDefaultAdapter();
$db->insert($this->_name, $fields_values);
$idAddress = $db->lastInsertId($this->_name);
Regards
Andrea
i am using this...
db = Zend_Db_Table::getDefaultAdapter();
$lastInsertId = $db->insert($this->_name, $fields_values);
The Zend_Db_Adapter_Abstract::lastInsertId() method is a proxy to PDO::lastInsertId(). According to the documentation:
PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
Note:
This method may not return a meaningful or consistent result across different PDO drivers, because the underlying database may not even support the notion of auto-increment fields or sequences.
Now you know. Use it as your own risk!
public function saveData(array $data) {
$this->_setName('user');
// this will return all field name for user table in $field variable
$fields = $this->info($this->getConstCol());
// By this foreach loop we will set data in $data array
foreach ($data as $field => $value)
{
if (!in_array($field, $fields))
{
unset($data[$field]);
}
}
// It will call insert function of (Zend_Db_Table_Abstract Parent of Zend_Db_Table)
// It will return $pkData which is primary key for user table
return $this->insert($data); // It will return last insert ID
}
I get a really anoying error when I try to edit an entry from a table, in tutorial they always use getTable()->find(), but I need to verify that the person logged in is the owner of that entry here what I did:
In the action:
public function executeEdit(sfWebRequest $request)
{
$id = $request->getParameter('id');
$userid = $this->getUser()->getGuardUser()->getId();
$ad = Doctrine_Core::getTable('BambinbazarArticles')->getMyAd($id, $userid);
$this->forward404Unless($ad, sprintf('Object bambinbazar_articles does not exist (%s).', $request->getParameter('id')));
$this->form = new BambinbazarArticlesForm($ad);
}
In the model:
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid);
return $q->execute();
}
I tried it with and without the ->execute(), did doctrine clean, cleared cache, rebuilded model,
Always get the same error 'The "%s" form only accepts a "%s" object.
If I use the Doctrine_Core::getTable('BambinbazarArticles')->find() it work, but of course, i need more than that..
I am becoming crazy over this.
execute() can return multiple rows; effectively you're getting a recordset back, rather than the individual object that your form is expecting. Try fetching a single object, using, e.g.:
return $q->execute()->getFirst();
or
return $q->fetchOne();
Its probably because your query is returning a Doctrine_Collection, not the actual Doctrine_Record youre expecting. Instead of execute use fetchOne.
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid)
->limit(1);
return $q->fetchOne();
}