I am not sure if this is the correct way to use this framwork.
I have a table books ids, there is also containing a list of terms these can be added to e.g. name author etc.
I also have a table which contains the term data, so for example I have a term called 'Name' this has an id of 1, so in the term data table there would be termdataid, bookid, termid, value.
How can I represent these in a view?
At the moment I have created a new model called 'termholder' this allows me to hold the termid, bookid, value, termname.
$termfeilds= \app\models\TermField::findAll(['active'=>'1']);
$_arrayterms = [];
foreach($termfeilds as $termfeild){
$_t = new \app\models\termholder();
$_t->name = $termfeild->name;
$_t->id = $termfeild->id;
$_arrayterms[] = $_t;
}
foreach($_arrayterms as $_arrayterm){
echo '<tr>
<td>'. $_arrayterm->name.'</td>
<td>'.$form->field($_arrayterm, 'value')->textInput(['maxlength' => true])->label(false).'</td>
</tr>';
}
This works fine however all the inputs have the same name so when it validates it teats them all as the same element.
I am going around this the correct way?
Related
I have a case where user uploads a file containing large number of rows(let's say 1000). Each row contain information about a user.
This data is turned into PHP array with objects
E.g
$userArray = [{first_name: 'Peter', last_name:'Pan', email 'peter#example.org'},
{first_name: 'David', last_name:'Hasslehof', email 'david#example.org'}...]
Now for each row I would have to create
foreach ($usersArray as $user) {
$createdUser = User::create(array('email' => $user['email'], 'pin' => $user['id_code']));
$profile = Userprofile::create(array('first_name' => $user['first_name'], 'last_name' =>$user['last_name']));
$createdUser->profile()->associate($profile);
$createdUser->save();
$usergroup->addMember($createdUser);
}
This would mean that if I had 1000 rows, atleast 4000 queries, which is obviously too much. Is there a eloquent way to do this more elegantly?
I tried using querybuilder and insert first all profiles and then users but this did not work, because I dont know which profiles to retrieve(first_name and last_name are not unique fields) and therefore cannot link profile_id's to users that I would like to create.
I think you can use this this laravel library for import.
However, to import multiple relationships, i think there is no other way than use associate() or sync().
$model_object = new YourModelHere();
//your model id here
$model_object->id = $sheet->id;
//your model id fields
$model_object->field_1 = (int)$sheet->field_1;
$model_object->field_2 = (int)$sheet->field_2;
//related id of models heres
$id_of_related_1 = (int) $sheet->id_of_related_1 ;
$id_of_related_2 = (int) $sheet->id_of_related_2;
//in your Model.php the relation i.e. hasMany or Belongs to must be declared first.
$model_object->relationToModelOne()->associate(RelatedModelOne::find($id_of_related_));
$model_object->relationToModelTwo()->associate(RelatedModelTwo::find($id_of_related_2));
$model_object->save();
I have made a custom dropdown field in leads module. Its a dynamically fetching users from users table from the leads module as key => value pair.
The field works fine but when in the edit mode (create a new lead)...the value is not getting stored and instead the key is getting stored not value..
I mean like instead of 'James Bond' the id is getting stored ..which is like '7896877'
Now the funny thing is that in the detail view in sugarcrm (leads module) the name is displayed properly as i wanted it to work. ONly in the list view it displays the ID and also in the database its getting stored as KEY i.e the hash ID.
This is the function:
function getUSERS($bean) {
$resultArray = Array();
$query = "select id,(first_name + ' ' + last_name) AS Name from dbo.users ORDER BY first_name ASC";
$resultArray [''] = '';
$result = $bean->db->query($query);
while ($row = $bean->db->fetchByAssoc($result)) {
$resultArray[$row['id']] = $row['Name'];
}
return $resultArray;
}
Dropdowns in Sugar work as key/value pairing, the key is what is stored in the database and Sugar does the appropriate lookup to display the value. Except the list view seems to work differently for dynamic dropdowns.
Instead of building your array as $resultArray[$row['id'] ]= $row['Name'] you could use the username -$resultArray[$row['username'] ]= $row['Name']as usernames have to be unique but will be more meaningful to your users in the list view.
However, is there any reason you're not using a relate field to the Users module? That should solve all your problems without any coding.
We need to extract specific fields, no matter if they have data or not.
If I f.ex. want an putput with only the field with external id : 'logo' i am trying this:
$limit = 10;
$app_id = xxxxxxx;
$items = PodioItem::filter($app_id,array('Limit' => $limit,'external_id' => 'logo'));
Within podio not all the fields are filled in, and the result is that we do not get a return value for field 'logo'.
When we don't get a return from field logo - the array output is not structured in a way so we can grab the data we need.
How do we achieve this ?
Added text
Rough Example on print output:
items[0] = Companyname,Logo,Address,Phone,Country
(has data in Logo)
items[1] = Companyname,Address,Phone,Country,ContactPerson
(no data in Logo)
items[2] = Companyname,Address,Phone,Country
PROBLEMS ARISING
items[2][4] is not existing (but i won't know this)
items[0][2] is Address Field (but i won't know this)
items[1][2] is Phone Field (but i won't know this)
Looking for Company Contact Person [x][4] accross items will end in
[0] = Wrong (country)
[1] = Right data (ContactPerson)
[2] = PHP error
Like in SQL, i would take the coloumn_name_1,coloumn_name_2, etc.. and grab data from only these fields.
PodioItem objects have a collection of item fields. As you've noted only fields that have values are included (since including empty fields is redundant). As you've noted you can't reliably access the fields by their array offset.
What you should do it access it by field_id or external_id. These are the unique identifiers. You can use the field method on the PodioItem object for this purpose:
$items = PodioItem::filter(...);
foreach ($items['items'] as $item) {
// Get field with external_id "logo". You can also pass in field_id
$field = $item->field('logo');
if ($field) {
print "Found a logo field!";
}
}
I have a taxonomy vocabulary kategorie like this:
Kat01
Child01
Child02
Kat02
Child01
Child02
The vocabulary has a custom field with IDs like this: 957214d2-ce39-423d-aeb1-32f2e8b972f6, so every term and parent term has an ID (which is NOT the tid!).
I have a content type with a referenced taxonomy field kategorie. Also there is an array field called kategorieTempId, and the values are the same like in the kategorie-taxonomy. The number of IDs in that array represent the hierarchical order of the terms and parent terms. So, if the node should be in kategorie -Kat01 -> Child01, there would be two IDs in kategorieTempId, one for the parent, one for the child term.
Now I need to get the ID of the child term, than get the taxonomy term from the vocabulary and write it into the referenced term field.
What I did so far:
Set up a rule, which is fired before saving a node, using the action Fetch entity by property to get the taxonomy terms from the vocabulary into an array. Then I add an action Execute custom PHP code to find the child tid and write it into a field called katReturn.
Here is the codeI use (I'm a total php-beginner, and I don't know, if this is an elegant way to do it, but it works):
$anzahl = 0; //counter for hierarchy
foreach ($kat_id_fetched as $kat_term) { // fill two arrays
$tid[$anzahl] = $kat_term->tid; // array with termID
$parentTerm_id[$anzahl] = db_query("SELECT {parent} FROM {taxonomy_term_hierarchy} WHERE tid = $kat_term->tid")->fetchField(); // array with parent-termID
++$anzahl;
};
foreach ($tid as $item) { // check if tid is parent
$anzahl = 0;
if (!in_array($item, $parentTerm_id)) {
$kat_child = $item; // if not in parent-array, save tid
$node->field_kat_return['und'][0]['value'] = $item;
break;
};
++$anzahl;
};
So now I know the tid of the child term and I want to write it into the reference taxonomy field, but I don't get it. The code I try is:
foreach ($kat_id_fetched as $kat) {
if ($kat->tid == $returned_kat) {
$node->field_kategorie[] = $kat;
break;
};
};
I hope, I made clear what I want. As I said before, my php-skills are limited and I appreciate any help, which points me to the right direction. Thanx.
The solution is simpler than I thought. Thanks to #Clive
I can save the term-id, which is stored in $item, directly without going through a third foreach-loop.
$node->field_kat_return['und'][0]['tid'] = $item;
I am trying to implement a very simple search function with PHP in symfony.
Basically I have a form that posts a query and I want to retrieve the items in the database that match the query.
If I have a User table with columns first_name and last_name, I want to be able to retrieve all items that contain a query. For example, if I submit 'a', I will get all names that have an 'a' in them:
Bat Man
Black Beard
Adam West
Mister A
So I know I can get all objects in the table whose first names contain 'a' by specifying criteria:
$c = new Criteria();
$c->add(UserPeer::FIRST_NAME, '%a%', Criteria::LIKE);
$users = UserPeer::doSelect($c);
Is there a way I can pass a variable like $query in the add() function? Can I acquire $query via a form and then pass it as a variable in the add function and get all the objects that contain it? Am I even going about this the right way?
On your form create an input with the id 'query'
<label for="query">Query:</label>
<?php echo input_tag('query') ?>
In the action get the parameter IF the form has been submitted
then pass it into your criteria
if($this->getRequest()->getMethod() == sfRequest::POST)
{
$query = $this->getRequestParameter('query');
$c = new Criteria();
$c->add(UserPeer::FIRST_NAME, '%'.$query.'%', Criteria::LIKE);
$users = UserPeer::doSelect($c);
}