The only code i found is:
class modulenameComponents extends sfComponents
{
public function executeAction(sfWebRequest $request)
{
$object = $this->getContext()->getController()
->getAction($this->getModuleName(), $request->getParameter('action'))
->getRoute()->getObject();
}
}
But it doesn't work if component includes in different module.
The component is called from the template, so pass your object from the template:
<?php include_component('modulename', 'actionName', array('myObject' => $myObject)) ?>
You will be able to access your object in your component via $this:
class modulenameComponents extends sfComponents
{
public function executeActionName()
{
// your object
$object = $this->myObject;
...
}
}
Regarding Symfony 1.4.x doctrine admin generator where everything is done in generator.yml, components receive the form object, which contains the model object. So, to get a hold of the current model object, do the following:
$myModelObject = $this->form->getObject();
NOTE: This is the model object, not the table object. To get the table object, do this:
$myModelTableObject = $this->form->getObject()->getTable();
I had to figure this out myself, so hope it helps someone...
There may be a better way, but using sfConfig::set() in your action an sfConfig::get() in your component should do the trick.
Related
It's more a "global understanding" question.
To save a model instance in the Database, we can use both:
SAVE()
$model = new Model;
$model->attribute = value;
$model->save();
https://laravel.com/docs/5.4/eloquent#inserts
and
::CREATE()
App\Model::create(['attribute'=>'value']);
https://laravel.com/docs/5.4/eloquent#mass-assignment
I supposed both of these methods belong to Illuminate\Database\Eloquent\Model, but I have found only function save there:
public function save(array $options = [])
{
$query = $this->newQueryWithoutScopes();
//......
return $saved;
}
But I haven't found any function Create in that file.
My QUESTIONS are:
1) what is the fundamental difference between
->method()
and
::method()
(is the last one a query builder?)
2) where can I find "::create()" method declared?
Thank you very much!
::method() is static calling without the need of creating an object of the class beforehand. ->method() you have to create an object before.
$car = new Car();
$car->color = 'red';
$car->save();
vs.
$car = Car::create(['color' => 'red']);
The create method can be found:
\Illuminate\Database\Eloquent\Builder::create
1)
->mehtod() is calling a Non-Static or Instantiated object method. Where as ::method() is calling on a static public method of a class.
To help describe this in your context. Take a look at how ::create() Operates. It returns an object that you can now use the save() method on after making changes. In the inverse, you cannot 'create' a model object from the save() method. You must have a model object first before executing ->save(). Which where ::create() comes in.
Eloquent ORM - Laravel : Insert, Update, Delete
2)
the create method is declared, I believe, in a higher level.
So I have a very simple (at least right now it is) model that returns a contact via it's primary key ID:
class Model_Contact extends \Fuel\Core\Model
{
public function get_by_id($contact_id)
{
return Entity_Contact::find_by_pk($contact_id);
}
}
The Entity_Contact class looks like this (irrelevant array content omitted):
class Entity_Contact extends \Core\Entity_Base
{
protected static $_table_name = 'contacts';
protected static $_properties = array(...);
protected static $_public_settable_properties = array(...);
protected static $_rules = array(...);
}
Note: \Core\Entity_Base extends \Fuel\Core\Model_Crud
I might use this in a controller like so:
$model = new Model_Contact();
$contact = $model->get_by_id(4);
I know that in order to unit test this, I should mock out the actual database call (Entity_Contact::find_by_pk), but I'm not sure how to do this. Since I'm using Fuel's Model_crud functionality (where the DB accessors are actually a part of the domain object model), I'm not sure that I can completely mock the database---or maybe I'm missing something.
So the question: how would you write a test for Model_Contact::get_by_id()?
Thanks in advance!
You can mock it easily if you use AspectMock.
https://github.com/Codeception/AspectMock
Your test may have to create the objects so you can test your methods.
Create the objects that get stored to the database.
Test get_by_id
Delete your created object
It's not the most glorious thing in the world, but it would accomplish testing your method... meh?
I've been trying to learn cakephp recently but I'm struggling to find any tutorials that deal with storing data into a table after it's been modified. I'm used having complete control where everything goes in PHP, so it's been a struggle adjusting to the automated processe of MVC.
I thought a good first experiment would be to take an input and concatenate a letter to it(let's just say "m"). Then, store both the original value and the concatenated value in a table with fields "orignal" and "concatenated". So, if I typed "hello", the value in the original field would be "hello" and the concatenated field would be "hellom".
My question is would the model be responsible for concatenating the original value? Would it also do the saving or is that the controllers responsibility?
Here is my code: I'm getting the following error.
Fatal error: Call to a member function save() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/cake/app/Model/Concatenate.php on line 6
View/Concatenates/add.php
<h1>Add Something</h1>
<?php
echo $this->Form->create('Concatenate');
echo $this->Form->input('original');
echo $this->Form->end('Add Numbers');
?>
Now for the model
class Concatenate extends AppModel {
function saveConcat($original,$concatenated) {
$this->set(array(
'original' => $original,
'concatenated' => $concatenated));
$this->save();
}
}
?>
Now for the controller
<?php
class ConcatenatesController extends AppController {
public $helpers = array('Html', 'Form');
public $components = array('Session');
public function index() {
$this ->set('concatenates', $this->Concatenate->find('all'));
}
public function add() {
if ($this->request->is('post')) {
$original = $this->request->data['Concatenate']['original'];
$concatenated = $original."m" ;
$this->Concatenate->saveConcat($original,$concatenated);
}
}
function isempty(){ //used to check if there is data in the table. If there isn't any, "no data" will be displayed
$mysorts = $this->Concatenate->find('all');
$this->set('concatenates', $mysorts);
}
}
?>
This is the never ending debate (or preference) about fat model/skinny controller and vice versa.
As far as saving goes, the model should definitely handle the logic for that. Although, you would most likely call it from the controller like $myModel->save($data);
In concatenating values, I would personally handle that in the controller because it is business logic that isn't directly related to the model. For example, you may wish to concatenate a string and send it to the view instead.
[EDIT]
Disclaimer: I have almost zero experience with CakePHP but the fundamentals are the same.
You mentioned you can't get it to work, so one thing I am noticing is you have a function called Concatenate() in your model. This is the PHP4 style of constructors and is no longer "best practice" (unless of course you are running PHP4 but why on earth would you be doing that). In fact, it is likely to be deprecated entirely in the near future. The PHP5 way of doing constructors is with the __construct() function. If you do decide to use a constructor, I'd make sure to call parent::__construct(); in it to ensure the parent AppController class loads correctly.
In looking at the Concatenate() method's functionality, I doubt you intend to have that as your constructor anyway. Rename that function to something clear like saveConcat(). Also, I'm not sure I would be using $this->request->data as your source in case you want to be able to reuse this function and call it with any value. In that case, I'd add a parameter to the function
class Concatenate extends AppModel {
function saveConcat($data) {
if ($this->Concatenate->save($data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
Then somewhere in your controller, you will have to actually call this function. Modify your add() function from your controller to be something like this:
public function add() {
if ($this->request->is('post')) {
// Put data into array for saving
$data[] = array( 'original' => $this->request->data );
$data[] = array( 'concatenated' => $original."m" );
// Call model function to save it
$this->Concatenate->saveConcat($data);
}
}
[EDIT 2]
I just can't figure out why I'm getting the error: Call to a member function save() on a non-object.
When you call $this->Concatenate->save from inside the Concatenate class, that means you are trying to access a variable inside the class called Concatenate and execute a function. Neither of which exist of course. The reason is you need to call the object itself as such:
$this->save("blah blah");
That method (I'm assuming is a parent method from the AppModel class) will be called referencing the current instance of the Concatenate object.
So basically I'm making a leap from procedural coding to OOP.
I'm trying to implement the principles of OOP but I have a nagging feeling I'm actually just writing procedural style with Objects.
So say I have a list of pipes/chairs/printers/whatever, they are all all listed as products in my single table database. I need to build a webapp that displays the whole list and items depending on their type, emphasis is on 'correct' use of OOP and its paradigm.
Is there anything wrong about just doing it like:
CLass Show
{
public function showALL(){
$prep = "SELECT * FROM myProducts";
$q = $this->db-> prepare($prep);
$q->execute();
while ($row = $q->fetch())
{
echo "bla bla bla some arranged display".$row['something']
}
}
and then simply
$sth = new show();
$sth->showAll();
I would also implement more specific display methods like:
showSpecificProduct($id)->($id would be passed trough $_GET when user say clicks on one of the links and we would have seperate product.php file that would basically just contain
include('show.class.php');
$sth = new show();
$sth->showSpecificProduct($id);
showSpecificProduct() would be doing both select query and outputing html for display.
So to cut it short, am I going about it allright or I'm just doing procedural coding with classes and objects. Also any ideas/hints etc. on resolving it if I'm doing it wrong?
As well as the model practices described by #Phil and #Drew, I would urge you to separate your business, data and view layers.
I've included a very simple version which will need to be expanded upon in your implementation, but the idea is to keep your Db selects separate from your output and almost "joining" the two together in the controller.
class ProductController
{
public $view;
public function __construct() {
$this->view = new View;
}
public function indexAction() {
$model = new DbProductRepository;
$products = $model->fetchAll();
$this->view->products = $products;
$this->view->render('index', 'product');
}
}
class View
{
protected $_variables = array();
public function __get($name) {
return isset($this->_variables['get']) ? $this->_variables['get'] : null;
}
public function __set($name, $value) {
$this->_variables[$name] = $value;
}
public function render($action, $controller) {
require_once '/path/to/views/' . $controller . '/' . $action . '.php';
}
}
// in /path/to/views/product/index.php
foreach ($this->products as $product) {
echo "Product ID {$product['id']} - {$product['name']} - {$product['cost']}<br />\n";
}
A better fit would be to implement a repository pattern. An example interface might be
interface ProductRepository
{
public function find($id);
public function fetchAll();
}
You would then create a concrete implementation of this interface
class DbProductRepository implements ProductRepsoitory
{
private $db;
public function __construct(PDO $db)
{
$this->db = $db;
}
public function find($id)
{
// prepare execute SQL statement
// Fetch result
// return result
}
public function fetchAll()
{
// etc
}
}
It's generally a bad idea to echo directly from a method or function. Have your methods return the appropriate objects / arrays / whatever and consume those results.
The scenario you are describing above seems like a good candidate for MVC.
In your case, I would create a class strictly for accessing the data (doing selects of product categories or specific products) and then have a different file (your view) take the output and display it.
It could look something like this:
class Product_Model {
public function find($prodId) { ... }
public function fetchAll($category = '') { ... }
public function search($string) { ... }
}
Then somewhere else you can do:
$products = new Product_Model();
$list = $products->fetchAll(37); // get all from category 37
// in true MVC, you would have a view that you would assign the list to
// $view->list = $list;
foreach($ilst as $product) {
echo "Product ID {$product['id']} - {$product['name']} - {$product['cost']}<br />\n";
}
The basic principle of MVC is that you have model classes that are simply objects representing data from some data source (e.g. database). You might have a mapper that maps data from the database to and from your data objects. The controller would then fetch the data from your model classes, and send the information to the view, where the actual presentation is handled. Having view logic (html/javascript) in controllers is not desirable, and interacting directly with your data from the controller is the same.
first, you will want to look into class autoloading. This way you do not have to include each class you use, you just use it and the autoloader will find the right file to include for you.
http://php.net/manual/en/language.oop5.autoload.php
each class should have a single responsibility. you wouldn't have a single class that connects to the database, and changes some user data. instead you would have a database class that you would pass into the user class, and the user class would use the database class to access the database. each function should also have a single responsibility. you should never have an urge to put an "and" in a function name.
You wouldn't want one object to be aware of the properties of another object. this would cause making changes in one class to force you to make changes in another and it eventually gets difficult to make changes. properties should be for internal use by the object.
before you start writing a class, you should first think about how you would want to be able to use it (see test driven development). How would you want the code to look while using it?
$user = new User($db_object);
$user->load($id);
$user->setName($new_name);
$user->save();
Now that you know how you want to be able to use it, it's much easier to code it the right way.
research agile principles when you get a chance.
One rule of thumb is that class names should usually be nouns, because OOP is about having software objects that correspond to real conceptual objects. Class member functions are usually the verbs, that is, the actions you can do with an object.
In your example, show is a strange class name. A more typical way to do it would be to have a class called something like ProductViewer with a member function called show() or list(). Also, you could use subclasses as a way to get specialized capabilities such as custom views for particular product types.
I have an issue that is quite annoying with symfony 1.2 and propel.
I have a model where I have implemented inheritance using the single-table strategy. So, here is an excerpt of my model:
Ad (id, posted_date, description)
then RealEstateAd(location, price, transaction_type) and JobAd(position, requirements, company) which inherit both from Ad.
I would like to display all ads, but I would like to display a RealEstateAd differently from a JobAd. To achieve this, I've used a partial for a RealEstateAd and a partial for a JobAd.
So, in the action, I did this:
$c = new Criteria();
$this->allAds = AdPeer::doSelect($c);
In the template, I check the class of each object:
$add = $allAds[$i];
if ($add instanceof RealEstateAdd)
//Use the RealEstatePartial
The problem is that class of an object in the $allAds array is sfOutputEscaperObjectDecorator.
So, nothing is displayed at all.
How could I deal with this issue? is there a way to get an array with objects which are actually of the class RealEstateAd or JobAd? How is the hydrating process carried out here?
sfOutputEscaperObjectDecorator has a raw method to get the undelying object.
Anyway, the best thing you can do is to have three different classes (i assume that real estates and job ads are Models)
class Ad { public function __toString() { print 'ad'; } }
class RealEstates extends Ad { public function __toString() { print 'realad'; } }
class JobAd extends Ad { public function __toString() { print 'jobad'; } }
so you can just call print $myAd; in your view without checking the object types.
(use polymorphism luke)
I don't know much about symfony or propel, so if i'm way off base here i apologize and just ignore this post...
What if you create a helper function getAdType() that uses some methodology to distinguish between the different types of ads.
function getAdType( $ad ) {
if ( isset( $ad->position ) ) {
return 'job';
}
elseif ( isset( $ad->transaction_type ) ) {
return 'realestate';
}
}
$add = $allAds[$i];
if ( getAdType( $add ) == 'realestate' )
//Use the RealEstatePartial
I might be misunderstanding something, but unless you have overloaded AdPeer::doSelect(), then it will only return an array of instance of Ad.
If you were to post your schema, it would be easier for me or others to help as it is not really clear how you've built your object model. Is RealEstateAd a propel class defined in schema.yml? or is it a custom class you've added to lib?
Eitherway, AdPeer::doSelect* will only return Ad, so it sounds like what you need is a custom retriever in the AdPeer. Again, more info about your schema will help.