i want to load models from helpers... im using eval to insert the modelname etc.
public function getMod($mName) {
// this works but need a dynamic one
$model = $this->users = new Application_Model_Users();
// so i did this:
$model = $this->_listsMod = eval ("new Application_Model_$mName();");
return $model;
and you can call it by: $this->_helper->getmod->getMod('Users')->myuserFunc();
but it doesnt work, its says Fatal error: Call to a member function myuserFunc() on a non-object
Updated answer:
Have you tried:
$modelName = 'Application_Model_'.$mName;
$model = $this->_lists = new $modelName ;
?
You should have a look at the Factory Pattern (which is effectively what you're doing here) eg here and here.
As you probably know, its best to avoid using eval if possible. Also, it seems a bit odd needing to load Models like this - can you tell us why you need to load them like this?
Note: I've updated this answer following comments below, to make it quicker for future readers of the question / answer.
Related
So... I've inherited this rather large project based on Zend Framework 1.12 and a feature that I'm trying to add involves a more complex database operation than what I'm used to doing with the project. I'm extremely new to Zend Framework, or MVC for that matter. I found an answer for Zend Framework 3 which would have been perfect but I have to make do with this version.
The function basically builds a Zend_Db_Select based on various parameters and the feature I'm trying to add will involve joining two different tables and checking if a specific combination exists in one or the other.
Here's what I have so far:
//SQL that I'm trying to do. Assume table1 and table2 are already joined.
//Ignore the imperfect syntax. I'm trying to get the concept across.
//SELECT * FROM (table1 joined to table2 by a common key)
//WHERE ( (table1.column1 = myParam1) AND (table1.column2 = myParam2) )
//OR WHERE ( (table2.column1 = myParam1) AND (table2.column2 = myParam2) )
public function buildSelect($params){
//Zend code starts here
//This one starts the Zend_Db_Select
$select = $this->select();
$table1Name = get_table_name_from_object($table1);
//lots of preexisting code here
//my code starts here.
$table2Name = get_table_name_from_object($table2);
$select->join($table2Name, "$table1Name.key = $table2Name.key", array('column1', 'column2', 'key');
//After I wrote this, I instantly realized why it won't work the way I intended it but putting it here to show what I tried at which point I got stuck.
$select->where("($table1Name.column1 = ?) OR ($table2Name.column1 = ?)",$params[1]);
$select->where( "($table1Name.column2 = ?) OR ($table2Name.column2 = ?)", $params[2]);
//more preexisting code below.
return $select
}
Obviously, if I tried this as is, the program will happily return results that include a combination of, say, an entry where param1 is in table1.column1 and param2 is in table2.column2.
I received some feedback from a friend and posting here for posterity.
They noticed my code already contains parentheses and recommended that I simply take advantage of orWhere() then write it like this:
$select->where("($tableName1.column1 = ?", $params[param1])
->where("$tableName1.column2 = ?)", $params[param2]);
$select->orWhere("($tableName1.column1 = ?",$params[param1])
->where("$tableName2.column2 = ?)",$params[param2]);
I looked in the dev guide and online but can't seem to find any code examples. I'm just trying to convert a lead into a contact.
For example I have a lead with ID: 7, what I'm trying to do is possibly modify a bean property to convert that lead into a contact (not sure if this approach is correct). Something like
$bean->convert("Contact");
$bean->save();
Thank you for your advice
In sugar/suite, conversion is a process which is copying a bean from one module to another module e.g. lead into contact.
I have been using this function to make it happen.
function outright_copy_bean($old_bean){
$new_bean = new $old_bean->object_name;
foreach($new_bean->field_defs as $key => $value){
if(in_array($key, array("id", "date_entered"))){
continue;
}
if($value["type"] == "link"){
continue;
}
$new_bean->$key = $old_bean->$key;
}
$new_id = $new_bean->save();
return $new_id
}
i'm trying to build an API for school project,
i think i have everything right, but for some reason, the code doesn't read the use lines.
use repository\PDOPersonRepository;
use repository\PDOEventRepository;
use view\PersonJsonView;
use view\EventJsonView;
use controller\PersonController;
$user = 'root';
$password = '';
$database = 'wp1';
$pdo = null;
try {
$pdo = new PDO("mysql:host=127.0.0.1;dbname=wp1",
$user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$personPDORepository = new PDOPersonRepository($pdo);
$personJsonView = new PersonJsonView();
$personController = new PersonController($personPDORepository, $personJsonView);
$eventPDORepository = new PDOEventRepository($pdo);
$eventJSonView = new EventJsonView();
$eventController = new EventJsonView($eventJSonView, $eventPDORepository);
Fatal error: Class 'repository\PDOPersonRepository' not found in C:\xampp\htdocs\Werkpakket1\app.php on line 22
phpstorm doesn't give errors in the code, so i don't really understand why it does that
The use lines include classes based on their namespace rather than their physical location. It is different from include or require. If the use keyword is to find the classes, the code of those classes has to be included first.
Another approach is to use a custom autoloader which could match namespaces with the directories of the classes, so that they are loaded automagically only when you need (use) them.
In other words, your code above needs to be prepended with something like:
inculde 'Person.php';
Where that file would contain something like:
namespace repository;
class PDOPersonRepository { ... }
You could use multiple namespaces in a single file, but that is not common practice.
I am quite confused how to use partialLoop
Currently I use
foreach ($childrenTodos as $childTodo) {
echo $this->partial('todos/_row.phtml', array('todo' => $childTodo));
}
$childrenTodos is a Doctrine\ORM\PersistantCollection, $childTodo is a Application\Models\Todo
I tried doing
echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
->setObjectKey('Application\Models\Todo');
But in the partial when I try to access properties/functions of my Todo class, I cant seem to get them always ending up with either call to undefined method Zend_View::myFunction() when I use $this->myFunction() in the partial or if I try $this->todo->getName() I get "Call to a member function getName() on a non-object". How do I use partialLoops?
Try this
echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
->setObjectKey('object');
Then in your partial you can access the object like this
$this->object
object is the name of the variable that an object will be assigned to
You can also do this once in your Bootstrap or other initialization class if you have access to the view object like so
protected function initPartialLoopObject()
{
$this->_view->partialLoop()->setObjectKey('object');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setView($this->_view);
}
I also had "Call to function on non object" error when trying suggested syntax, seems like they've changed something on later versions of Zend Framework. The following works for me on ZF1.12:
echo $this->partialLoop()
->setObjectKey('object')
->partialLoop('todos/_row.phtml', $childrenTodos);
I'm trying to use a select object to filter the results of a many to many rowset. This call works great:
$articles = $this->model->findArticlesViaArticlesUsers();
This however does not:
$articles = new Default_Model_Articles();
$articleSelect = $articles->select();
$articleSelect->where("status = 'published'")
->order("date_published DESC")
->limit(1);
$articles = $this->model->findArticlesViaArticlesUsers($articleSelect);
That throws the following error:
exception 'Zend_Db_Select_Exception'
with message 'You cannot define a
correlation name 'i' more than once'
I can't figure out how to successfully get "articles that have the status of 'published'" using the magic many-to-many relationship (nor findManyToManyRowset). I'm at the end of my rope and thinking of just writing the sql manually. Any ideas?
When defining the select statement, you must use the same object that you call findManyToManyRowset (or whatever magic function you use) on.
Ex:
$articles = new Default_Model_Articles();
$user = $articles->find($userId)->current();
$select = $user->select();
$select->where('status = ?', 'published');
$articles = $user->findArticlesViaArticlesUsers($select);
Notice the select statement and findArticlesViaArticlesUsers are both extending $user. Thats the key.
I think you've misunderstood how the relationships work.
See this manual page - you should call the magic method, findArticlesViaArticlesUsers, on a Row object. In this case, I think you want to find a User, and then call findArticles... on that.