blog from shortname

News and discussion about beta versions and CVS changes
User avatar
matt_t_hat
Posts: 1123
Joined: Sun Aug 21, 2005 4:45 pm
Location: UK
Contact:

blog from shortname

Postby matt_t_hat » Sun Sep 11, 2011 7:06 pm

The MANAGEr class contians a number of passthrough functions to the BLOG class one of which does not make quite as much sense to me as the others.

I understand existsID returning true if that blog ID exists because then using the manager to ask for the blog object by ID should go ahead.

Less sensible seems to dot he same by shortname without also telling what the assoiated ID is. So my code can be pretty damn sure that blog something exists but have no idea short of running some SQL itself what the ID is.

This fix would in theory fix that.

Code: Select all

   // returns blogid (true) if there is a blog with the given shortname (static)
   function exists($name) {
      $r = sql_query('select `bnumber` FROM '.sql_table('blog').' WHERE bshortname="'.sql_real_escape_string($name).'"');
      if(sql_num_rows($r) != 0){
         $row=mysql_fetch_row($r);
         return $row[0];
      }else{
         return false;
      }
   }


It now returns the blogid which would evaluate as non-false in all existing code and yet can be inspected to get the ID for going further without any additional SQL or fuss.

This is exceptionally more useful for me and I am sure anyone else who wants to do rather advanced stuff with blogs and would not change the way anything else worked.

I've taken to implementing the function in plugin at the moment but the purest in me is not pleased with my hackery.
ftruscot
Nucleus Guru
Nucleus Guru
Posts: 7430
Joined: Wed Feb 22, 2006 6:19 pm
Location: Massachusetts
Contact:

Postby ftruscot » Mon Sep 12, 2011 5:10 pm

I like the theory of this Matt. One subtle difference between your proposed BLOG::exists($name) function and the existing one is that they would evaluate differently to the following conditional (assuming that a blog with shortname of 'fred' DOES exist):

Code: Select all

if ( BLOG::exists('fred') === true ) echo "YES";
else echo "NO";

The existing code would echo "YES", yours would echo "NO".
Now, I doubt this "identical" comparison operator is used in this case, but we'd need to check for it.

If I'm looking to see if a blog exists (by name) and then load the object, I might use the getBlogIDFromName($name) function to test the existence of the blog and get the id, like this:

Code: Select all

global $manager;
$name = 'fred';
$myBlogId = intval(getBlogIDFromName($name));
if ( $myBlogId )
{
    $myBlog = $manager->getBlog($myBlogId);
}
else
{
    echo "No blog exists with a shortname of $name.";
}

I'll look into to the suggested modification though.
Is your question not solved yet?
Search our FAQ,
read the Documentation, or
browse the list of available plugins.

Check out my plugins
User avatar
matt_t_hat
Posts: 1123
Joined: Sun Aug 21, 2005 4:45 pm
Location: UK
Contact:

Postby matt_t_hat » Tue Sep 13, 2011 1:06 pm

It clearly pays to have a better working knowledge of the insides of NucleusCMS. Thanks for your feedback ftruscot. You are a star.

Return to “Core Development”