I have an issue I keep hitting the wall on and I need some suggestions on the best way around this.
I got a loop that grabs data from my model, but within that loop, I have to validate it against a library, but I cant seem to find a good way to do that.
for example, I got a model method that looks similar to this:
public function GetUserList() {
$this->db->select('username, email, joindate, rank')->from('user');
$query = $this->db->get();
//loop through data and bind to an array.
foreach ($query->result() as $row) {
$users[] = $row;
}
return $users;
}
when the page loads the user will see a list of users and their info, but some users don't want their email address to be shown, we then have to use the user library to fetch the settings for that user and see if we should hide that email or not.
the only solution i found was to use a helper function as a detour, but I feel that is going to bite me in the future duplicating things like that.
the other issue I found was once you created the library it didn't restart the object as a new one (like it would in vanilla PHP).
I'm hoping someone has a good way to resolve a setback like this.
One last item, if it makes a difference, I'm also using Twig to handle my views.
You do not need to load every library file using the load() function. The load() function is for loading a single instance in the global (CI) namespace. If you want to work with a collection of objects, you should require the file like you normally would in PHP.
I would personally join on your preferences table if possible. That way you wouldn't have to do a separate query for every single user.
Related
If I had a menubar that was totally dynamic where you can add pages to it and remove pages from it (being stored in database) . These pages all have content in a database , things like an article however I cant store PHP code in the database so I cant add things like dynamic tables within the page, is there any way around this or am I missing something. I am using Laravel. Feel free to ask to elaborate as I dont think this is as concise as it could be.
First of all: Saving php code into a database, or doing anything requireing eval is usually wrong code.
What you could do is make classes which renders the right dynamic view, Make an array of anonymous functions which can instantiate the classes, and put the array keys into the database.
your code would look like this:
class yourrenderClassLol{
function render(){echo"lol";}
}
$classes["yourrenderClass"]=function(){
static $instance=NULL;
if($instance==NULL){
$instance=new yourrenderClassLol();
}
return $instance
}
$classes["yourrenderClass"]()->render();
Looking for a bit of advice.
I have an update script for my website’s database, but as yet - I’ve not been able to figure out how to convert it to the Codeigniter framework ie. it’s saved as it’s own file in the home directory of my website, and executed from there.
There’s a couple of things making it difficult for me to figure out:
It uses php xpath - not difficult to see how I would convert this to a Codeigniter controller, but I’m wondering if update_batch would be a better solution for me as opposed to using a foreach loop as each row is update by just 1 index column. There’s next to no documentation available on this however…
My code utilises MySql’s On duplicate feature which as I understand is not supported in Codeigniter.
Code:
$string = 'http://mywebsiteetc.com/xml/gzip/';
$xml = simplexml_load_file("compress.zlib://$string");
foreach ($xml->xpath('//merchant') as $row)
{
$merchant_id = $row['id'];
$merchant_name = $row['name'];
mysqli_query($con, "INSERT INTO merchants (merchant_id, merchant_name) VALUES ('$merchant_id', '$merchant_name') ON DUPLICATE KEY UPDATE merchant_id = '$merchant_id', merchant_name = '$merchant_name'");
}
Can anyone steer me in the right direction as to how to implement the above into a controller/model environment - and of course, advise me of the most efficient way to do all of the above if there is one?
Cheers!
Basically you need to know more about the MVC structure of Codeigniter, you can start reading here for the controller.
In your setup:
create a file in controller app/controllers/update.php
in your update.php you can write your functions in public function index()
Parse your xml and pass it in Model (create a model for this setup)
then save/inserts it one by one or per batch , put the code in your Model
Again you have to read the documentation for a better understanding how to separate your MVC and make it a one functionality
I'm currently attempting to generate form elements in Symfony based on the application's model. I know the code below is invalid, but I'm hoping it shows what I'm trying to do. I've really hit a wall with it... I can't seem to wrap my head around how I'd utilize the form library to accomplish this.
$formElementArray = FormElementsTable::getInstance()->getElementList($advertiserID);
$formElements = array();
foreach ($formElementArray as $formElement)
{
$formElements['name'] . => new sfWidgetForm.$formElements['type'] . (array('label' => $formElements['label'])),
}
$this->setWidgets(array($formElements));
My concern right now is that this isn't possible and I should abandon the idea, perhaps write a symfony component which produces forms from the model. That would actually be fairly simple, but I suppose I'd just like to stay as deep within the framework as possible for several reasons.
Thanks for any input.
edit: A bit more information...
The purpose is to be able to populate a form with whatever elements appear in a certain row of the database. For example, perhaps advertiserX needs a name, sex, and hair colour field, while advertiserY needs a name, sex, weight, and eye colour field. I need to be able to produce those on the fly, ideally with symfony's form features available.
Solution
This is how the form is configured...
public function configure()
{
$elements = Doctrine_Core::getTable('FormFields')->getElementsById(0);
foreach ( $elements as $element )
{
$widgetName = $this->getWidgetName($element);
$args = $this->getConstructorArguments($element);
$widgets[$element->name] = new $widgetName($args);
$validatorName = 'sfValidatorString';
$validators[$element->name] = new $validatorName(array('required' => false));
}
$this->setWidgets($widgets);
$this->setValidators($validators);
}
Simple, right? I shouldn't have over thought it so much.
It is best pratice to generate forms from your model, because if you change your models, you'll probably want to change your forms too. Doctrine and Propel both support this, so don't reinvent the square wheel : use what already exists ;)
UPDATE
In your case, maybe the model can be the same for all the advertisers, and all you need to do is extend the form to make the small variations appear in the forms. Your classes will call parent methods of the generated form, and slightly change widgets and validators.
If you intend to store the data sent when filling the user form (not the form form), then I think you should use a NoSQL system because your schema is dynamic (it changes when the advertiser changes). Read this blog post to learn more about them. Also read this SO post to learn what can be used with symfony.
I'm currently writing an EPOS integration for Magento. When an order is made, it's ID is placed in a queuefile. Once a minute, a cron looks at the queue, fires the top order to the EPOS web api and then moves the ID to either a successlist file or a faillist file, depending on the outcome.
In order to display the contents of these lists to the user, I created an admin page that reads the file (containing a serialized array), creates a varien_object containing the order ID, customer name and timestamp for each order, and then stores all of these in an instance of a Varien_Data_collection. This collection is then passed to the _prepareCollection function in the grid.php for rendering the grid view.
In 1.4.1.1, the grid renders fine, but the pagination is broken and the filtering doesn't work.
In 1.3.2.4 the grid renders but says there are 'No Records Found'.
Does anybody know what could be causing these issues, and if there is a better way of going about displaying information from a file in Magento?
The reason why you can see the entries (1.4+), but can't filter is that Magento is using the collection api to modify the object. If you are just pulling values out of a model, its no big deal, but if you are searching and filtering, Magento needs the collection to be an instance of a database. It uses Varien_Db_Select objects to make queries which resolve to raw sql, so that's not going to work on an array.
I would recommend trying to deal with the data in a different way.
It sounds like you are working with a flat file, so the obvious solution of constructing a sql query to fetch everything for you won't cut it. I would try creating an instance of Zend_Db_Table, and populating the values on the fly.
Something like this:
class Foo_Bar_Model_Resource_Success_Collection extends Mage_Core_Model_Resource_Db_Abstract
{
public function _construct()
{
//declare write adapter in config
$table = new Zend_Db_Table('my_db.my_table');
foreach($this->getEposArray() as $entry)
$table->insert($entry);
$this->_init('my_table', 'id');
}
}
Admittedly, I've never done anything quite like this, but have had the custom grid filter problem crop up on me before, and know that if you want to search, you need to have your data in a table of some sort. Check out Zend's docs on the matter. I'm pretty sure that there's a way to do this inside of Magento, but I couldn't begin to think about a solution.
My advice, store your cron job data in a database, it will make pulling the data back out much easier.
Is there a codeigniter plugin that allows me to quickly create find by functions without writing code on every field in a db table?
I find myself writing a lot of functions for tables such as findbyid findbyfirstname findbyemail and so on, any libraries already written to speedup my dev time? i tried googling but i havent come across any.
If you mean you have to write multiple methods in your model to find rows in a table by a specific field, you could just pass an associative array containing the fields and values you want to search to a generic function - something like:
function search_mytable($search=array()) {
$this->db->select('mytable.*');
$this->db->from('mytable');
if(!empty($search)
$this->db->where($search);
}
There's more information about what you can pass the CI active record where method here http://codeigniter.com/user_guide/database/active_record.html#select
If it's just simple data retrieval, you can just do something like this:
function find($column, $value)
{
$this->db->where($column, $value);
//etc
}
for simple queries. As BrynJ suggests, the Active Record class is rather flexible when it comes to taking parameters.