CI 2 - Language class and models - codeigniter-2

I noticed an odd issue and I can't seem to find anything to explain it.
on one of my models i add an error output message to alert that a record is not available, but when it outputs, i get no text, just a blank page.
Now the language tags are valid, the only thing i can figure is that the language file isn't meant to be used on a model, if thats the case I don't see that being explained anywhere.
I hope someone can help me get in the right direction on this, if i have to just set it to false and handle the message on the ui, I'll do that, but prefer not to.
Thanks.
EDIT:
here is what a quick code snipplet that should help:
//fetch topic data.
$this->db->select('id, Name, Description, Level');
$this->db->from('groups');
$this->db->where('id', $gid);
$query = $this->db->get();
$GroupData = $query->row();
//see if we have any records to show.
if($query->num_rows() > 0) {
$this->setId($GroupData->id);
$this->setName($GroupData->Name);
$this->setDescription($GroupData->Description);
$this->setLevel($GroupData->Level);
} else {
//no record was found, throw an error.
show_error($this->lang->line('invalidgid').'<hr />File:'.__FILE__.'<br />Line:'.__LINE__, 500, $this->lang->line('error'));
log_message('error', 'invalid GroupID was provided.'); //log error in error log.
}

I feel dumb, but I figured it out, the language file hasn't loaded yet at that point, ARGH!
I will have to go back to the drawing board on this one it looks, any suggestions, please post them.

Related

How to show all data where date is earlier than today laravel

I`ve got code that shows all of the placements for a user. Is there a way to narrow this down to only show placements that are in the future? I've tried to do this using where and carbon::now to no avail.
My current code to show all of the placements :
$placements = Auth::user()->placementsAuthored;
$placements->load('keystage', 'subject', 'dates');
Placements Authored connection to connect a user to a placement :
public function placementsAuthored()
{
return $this->hasMany(Placement::class, 'author_id');
}
My attempt at trying to do this. I get no errors but the code doesn't work. It doesn't seem to take any effect of my where clause any ideas?
$placements ->where('date','>',Carbon::now()->format('Y-m-d'));
After a bit of tweaking, I found that this works but I don't understand why this works and the above doesn't. In my mind, they do the same but this is a longer way of doing it. Any idea why this works and the above doesn't?
// Only load future placements
$placements = Placement::whereHas( 'dates',
function ($q) {
$user_id = Auth::user()->id;
$q->where('author_id', $user_id)->where('date','>=' ,Carbon::now()->format('Y-m-d'));})->get();
You should take a different name for the date column because the date keyword is already reserved in the PHP function this is not a standard way

Why is Eloquent is dumping an array on save()?

// Look for exact matches
foreach (Game::where("console_id", $id)->get() as $game) {
$search = Info::where("game_title", $game->name)->first();
if ($search) {
$game->info_id = $search->id;
$game->save();
continue;
}
When I run the following code, Laravel simply dumps $game on $game->save(). I've also tried Game::find($game->id)->update(["info_id",$search->id]) and it's behaving the same way. It does actually save before it dumps but it stops execution immediately after.
Unless I'm going crazy, this isn't the expected behavior. I've searched up and down and haven't found anyone else with this issue.
Anyone have any ideas? Thanks!

XDocument.Validate how to keep validating after errors?

i have a little problem and can´t figure out how to resolve it.
I am Validating an XDocument against a schema and i get all the nodes which have error. But the Validation process doesnt go deeper after finding an error.
_document.Validate(_schema, (o, e) =>
{
XElement xEle = null;
if (o is XAttribute)
xEle = (o as XAttribute).Parent;
if (o is XElement)
xEle = o as XElement;
if (xEle == null)
{
Debug.WriteLine(o.ToString());
return;
}
_elemtList.Add(o as XElement);
});
My problem is like following
<Car>
<CarName></CarName>
<CarInteriour>
<CarInteriorColor>Red</CarInteriorColor>
</CarInteriour>
</Car>
Lets say this is valid.
If i Change the following to
<Car>
<CarInteriour>
<CarInteriorColor></CarInteriorColor>
</CarInteriour>
</Car>
Here is the CarName tag missing and the color Red.
I will only get the error for CarName but not color Red.
The validation process seems to skip that structure because it did find an error.
Is there a way to still keep validating even if there was an error ?
Ok i found a solution which works for me and i am giving you an update because it works pretty neat.
What i am doing now is. I use a foreach for every XElement in my _document.Descendants() and i validate every element. This finds every error in the document multiple times.
So what i do is, i check my errorlist if i already did find this error before. My errorlist is a list of my own class errorcontainer which has the found XElement with the Error Message.
This way i only add new errors to the list and show this errors on a dialog. The user now can select an error and i will directly jump to the error in my editor.
ErrorContainer errorContainer = new ErrorContainer(xEle, e.Message);
if (_errors.Any(error => error.xElement.Equals(errorContainer.xElement) && string.CompareOrdinal(error.errorMessage, errorContainer.errorMessage) == 0))
{
return;
}
_errors.Add(errorContainer);
I hope this helps some other people who need help on this issue :)

How can I retrieve the latest question of each thread in Propel 1.6?

I want to get the newest entries for each of my threads (private messaging system) with Propel 1.6 making use of the fluid ModelQuery interface. This would allow me to reuse both methods for getting newest entries and only getting entries where a user is involved (nobody wants to see messages not for him).
I already found out that in standard-SQL I have to use a subquery to get the newest entry for each of my forum threads. I also found out that in Propel you have to use a Criteria::CUSTOM query to achieve this, but the whole Criteria::CUSTOM stuff seems to be pre-Propel-1.6, because none of the examples makes use of the new ModelQuery.
Now the problem is, that I want to make use of the concenation feature in ModelQueries, where you can attach several own methods to each other like this:
$entries = MessageQuery::create()
->messagesInvolvingUser($user) // user retrieved or sent the message
->newestFromThread() // get the latest entry from a lot of Re:-stuff
I do not think that this would still be possible if I had to use
$c = new Criteria();
$c->add([the subquery filter]);
in newestFromThread().
What’s the best method to retrieve the latest entry for each thread given the following scheme (thread_id means that all messages belong to the same correspondence, I want only one entry per thread_id):
id(INT)
title(VARCHAR)
thread_id(INTEGER)
date(DATETIME)
The current PHP-implementation looks like this:
<?php
class MessageQuery extends BaseMessageQuery {
public function messagesInvolvingUser($user) {
return $this
->where('Message.AuthorId = ?', $user->getId())
->_or()
->where('Message.RecipientId = ?', $user->getId());
}
public function newestFromThread() {
return $this;
// To be implemented
}
}
And I am using it like this:
$messages = MessageQuery::create()
->messagesInvolvingUser(Zend_Auth::getInstance()->getIdentity())
->newestFromThread()
->find();
How about ordering results by date (DESC) and to limit to one result ?
Considering the answers in a similar question about pure SQL solutions, I guess it is easiest to add a new column newest indicating which message in a communcation is the newest. This probably fits the object-oriented approach of Propel better, too. I could write my application like this then:
public function preInsert(PropelPDO $con = null) {
$this->setNewest(1);
$this->getEarlier()->setNewest(0);
return true;
}

How do I filter a collection by a YesNo type attribute?

I have a ‘featured’ attribute, which has a Yes/No select-list as the admin input. I presume that the values for Yes and No are 1 and 0, as they are for every other Yes/No list. However, if I try and filter a collection using the ‘featured’ attribute, it doesn’t work:
$feat_attribute = $_product->getResource()->getAttribute($featuredattribute)->getSource()->getOptionId(1);
But, if I make a ‘featured’ attribute with a dropdown, and write my own Yes and No, then it works as below:
$feat_attribute = $_product->getResource()->getAttribute($featuredattribute)->getSource()->getOptionId('Yes');
Anyone any ideas? I’ve also tried values as true/false, yes/no, on/off etc, but no joy.
This seems to be an old thread, but anyway I just had the same issue, I set the attribute to be visible in product listing and product view, and then apply addAttributeToFilter(feature_product_attribute, 1) for Yes/No type.
Maybe you are supposed to use '1' and '0' instead of the integer-values?
Like:
$feat_attribute = $_product->getResource()->getAttribute($featuredattribute)->getSource()->getOptionId('1');
Whenever Magento's behavior is confusing me, I start hacking on the core source (a development copy, of course) to see what it's doing and why not doing what I think it should. I haven't done much playing around with the Admin UI stuff so I don't 100% understand your question, but take a look at the getOption function
File: /app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Abstract.php
public function getOptionId($value)
{
foreach ($this->getAllOptions() as $option) {
if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
return $option['value'];
}
}
return null;
}
I'd add some Mage::Log and/or var_dump calls in there for the values of $option['label'] and $option['value'] and see why your comparison is failing.

Resources