SugarCRM - insert new record in relationship table - sugarcrm

Hi I am testing and trying to insert a record in relationship table but without success. I am trying to enter new row in relationship table using logic hook when I do Save in Contacts module but instead of that I am getting blank index.php page.
Where is the problem?
thank you
<?php
include_once('modules/Contacts/ContactOpportunityRelationship.php');
class User_hook {
function insert()
{
$instance=new ContactOpportunityRelationship();
$instance->$contact_id='96163e07-f55a-eb87-251f-513482e4a1da';
$instance->$opportunity_id='c99178c5-fed5-7092-4a79-4e1da40a1eea';
$instance->save();
}
}
?>

The correct syntax should be
<?php
$contactId = '96163e07-f55a-eb87-251f-513482e4a1da';
$oppId = 'c99178c5-fed5-7092-4a79-4e1da40a1eea';
$oContact = BeanFactory::getBean('Contacts', $contactId);
$oContact->load_relationship('opportunities');
$oContact->opportunities->add($oppId);

Related

Laravel Eloquent BelongsTo() issue with echoing results

EDIT : I solved it. I'm leaving it here as a reference, I hope it helps someone in the future.
I'm trying to echo out a comment's owner like below, but it gives an error.
Trying to get property of non-object
$comments = ThingComment::where('thing_id', $id)>orderBy('comment_date', 'desc')->get();
blade template
#foreach($comments as $comment)
#foreach($comment->thingCommentOwner as $owner)
{{$owner->user_name}}
#endforeach
#endforeach
I have three models : User, Thing and ThingComment Thing probably isn't important, so I didn't include it.
My tables are : users and thing_comment
user_id and thing_id are foreign keys in the thing_comment table. I should be able to access the users table columns through the thing_comment table, that's what I'm trying to achieve here.
Models are below.
ThingComment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ThingComment extends Model {
protected $table = 'thing_comment';
protected $primaryKey = 'comment_id';
protected $fillable = ['comment', 'comment_date', 'upvotes', 'downvotes', 'user_id', 'thing_id', 'parent_comment_id'];
public function thingCommentOwner() {
return $this->belongsTo('\App\Models\User','user_id');
}
public function commentThing() {
return $this->belongsTo('\App\Models\Thing','thing_id');
}
}
They need to be used like below. thingCommentOwner allows us to JUMP to the users table, which is cool.
#foreach($comments as $comment)
{{$comment->comment}}
{{$comment->thingCommentOwner->name}}
{{$comment->thingCommentOwner->user_image}}
<p></p>
#endforeach

How to integrate web services with custom module in SugarCRM?

I'm using SugarCRM to develop a software for customers management. I created a custom module from basic template with custom fields. Is it possible to get rid of SugarCRM db and perform CRUD operations through external web serivices? Actually I was able to show web services data in the datailview by setting the bean property of a custom controller.
class CustomerController extends SugarController{
public function action_detailview(){
$customer = new Customer();
$customer = getCustomerFromWebService();
$this->bean = $customer;
$this->view = "detail";
}
}
I would like to do the same thing with listview, but I don't know how set the records of the list (if it exists) used by the default listview.
You can change list view by customizing view.list.php in custom/modules/modulename/views/view.list.php using following code:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('include/MVC/View/views/view.list.php');
// name of class match module
class modulenameViewList extends ViewList{
// where clause that will be inserted in sql query
var $where = 'like 'htc'';
function modulenameViewList()
{
parent::ViewList();
}
/*
* Override listViewProcess with addition to where clause to exclude project templates
*/
function listViewProcess()
{
$this->lv->setup($this->seed, 'include/ListView/ListViewGeneric.tpl', $this->where, $this->params);
echo $this->lv->display();
}
}
?>

Fetch multiple rows and display as form using yii

I have a database table worksheet(qID varchar(5), answer varchar(50), wsheetid varchar(5)) with qID as the primary key. One worksheet has many questions.
I want to fetch all the questions where wsheetid =1 and display them as a form so that user can enter the answers.
And i want to check the user entered ans with the answer column in the database.
How can i do that in Yii.
Tried google and the guide to yii, couldnt find any solution. Any suggestion would be helpful.
Update:
I have the below view where i am am needed to get the attributes into a array and display the form. Is there a better way to do this? and for activeTextArea since model has the data, the text area contains data from database but i need a blank text area.
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'wdetails-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo CHtml::beginForm(); ?>
<?php foreach($questions as $i=>$questions): ?>
<?php $array = $questions->getAttributes();
echo CHtml::activeLabel($questions,"[$i]question",array('label'=>"$array[question]"));
echo CHtml::activeTextArea($questions,"[$i]answer",array('id'=>"$array[question_ID]"));
endforeach;
?>
</br>
<?php echo CHtml::submitButton('Submit'); ?>
<?php echo CHtml::endForm(); ?>
<?php $this->endWidget(); ?>
</div>
and in the controller, i need to insert the data that i got from the above form and insert into a different table(worksheetResults). DO i need to get the data into an array and use Yii DAO or is there any better way to do this.
table worksheetResults (username, worksheetID,question_ID,submitted_ans)
I think you should have a Worksheet model, a Worksheet controller and a Worksheet view.
Here is a skeleton for the controller part:
class WorksheetController extends CController
{
public function actionQuestions()
{
$criteria = new CDbCriteria;
$criteria->addCondition('(wsheetid = :id)');
$criteria->params[':id'] = '1';
$questions = Worksheet::model()->findAll($criteria);
$this->render('questions_form', array('questions'=>$questions));
}
public function actionAnswers()
{
//check the contents of $_POST['Worksheet']
}
}
Did you use Gii to create the scaffolding? That would get you the model and CRUD files and you can then customize the look and functionality. If your data model is good, this will save you a lot of time, even if you end up redoing the form view, you can still use Yii to create and maintain the model and controller classes.
See: http://www.yiiframework.com/doc/guide/1.1/en/topics.gii

Zend - Add subForm to existing display group

In my controller I got a function which looks like this:
public function newExperienceAction() {
$this->_helper->layout->disableLayout();
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('newExperience', 'html')->initContext();
$id = $this->_getParam('id', null);
$this->form = new Application_Form_Cv();
$this->experience = new Zend_Form_SubForm();
$this->form->addSubForm($this->experience, 'experience');
$rowExperience = new Application_Form_Experience();
$rowExperience->setDisplayGroups('experience');
$this->experience->addSubForm($rowExperience, "experience$id", $id+3);
echo $rowExperience->__toString();
}
When the user press (+) on the form, a new subForm will be displayed.
I'm currently in the process if shaping it into a table. I will have more than one subForm on this form so I need to use DisplayGroups.
In this situation, I believe I have to create a Display group when the form is first created.
Then I need to append the new subForms to the existing display groups.
So the question:
How do I add new subForms to an existing display group?
You can achieve this by simply adding
$rowExperience->setIsArray(true);
This causes the sub-form to act like a display group, i.e. its values are wrapped in an array indexed by the sub-form's name (experience$id in your case).
I'm afraid this is not possible now. I'm using Zend Framework version 1.11.7 and the code of the addDisplayGroup method looks like that:
foreach ($elements as $element) {
if($element instanceof Zend_Form_Element) {
(...)
}
if (isset($this->_elements[$element])) {
(...)
}
}
So this is not supported yet. You can add only Zend_Form_Element instances

Symfony: How to hide form fields from display and then set values for them in the action class

I am fairly new to symfony and I have 2 fields relating to my table "Pages"; created_by and updated_by. These are related to the users table (sfGuardUser) as foreign keys. I want these to be hidden from the edit/new forms so I have set up the generator.yml file to not display these fields:
form:
display:
General: [name, template_id]
Meta: [meta_title, meta_description, meta_keywords]
Now I need to set the fields on the save. I have been searching for how to do this all day and tried a hundred methods. The method I have got working is this, in the actions class:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form_params = $request->getParameter($form->getName());
$form_params['updated_by'] = $this->getUser()->getGuardUser()->getId();
if ($form->getObject()->isNew()) $form_params['created_by'] = $this->getUser()->getGuardUser()->getId();
$form->bind($form_params, $request->getFiles($form->getName()));
So this works. But I get the feeling that ideally I shouldnt be modifying the web request, but instead modifying the form/object directly. However I havent had any success with things like:
$form->getObject()->setUpdatedBy($this->getUser()->getGuardUser());
If anyone could offer any advice on the best ways about solving this type of problem I would be very grateful.
Thanks,
Tom
After processing and saving the form you could set those fields on the object and re-save:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()));
if ($form->isValid())
{
$page = $form->save();
$user = $this->getUser()->getGuardUser();
$page->setUpdatedBy($user);
if (empty($page->created_by))
{
$page->setCreatedBy($user);
}
$page->save();
$this->getUser()->setFlash('notice', 'Successfully saved page.');
$this->redirect('#homepage');
}
}
There's also a Doctrine extension called Blameable that automatically sets edited_by and created_by fields on specified models. The Doctrine website is undergoing some reorganization but here is the cached page for the extension.
To process your form create a new object, set the fields then save.
$article = new Article();
$article->setName($request->getParameter($form->getName());
$article->setDescription($request->getParameter($form->getDescription());
$article->setMetaKeywords($request->getParameter($form->getMetaKeywords());
$article->save();
What you want to do is customize your form and unset the 'created_at' and 'updated_at' pieces of the form in configure
class SampleForm extends BaseSampleForm
{
public function configure()
{
unset(
$this['created_at'],
$this['updated_at']
);
}
}
Then they won't show up in the form and will get the values setup by the "Timestampable" behavior before being saved
http://stereointeractive.com/blog/2010/04/07/symfony-forms-hide-created_at-updated_at-columns/