Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

fletcher posted:

Or instead of reinventing the wheel you use PDO so other people that read your code don't think you are crazy.

PDO is nice and all, but you still need to write a half-dozen lines of code to do anything useful. I use a set of sprintf-like wrapper functions that automatically escape arguments, run the query, and return the first field of the first result, or the first result row as an array, or the result set, etc.

Adbot
ADBOT LOVES YOU

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

fletcher posted:

While those 6 lines are such a huge burden to type out (are you kidding me?) they are waaaaaay easier to read and debug.

There's no way that
code:
$name = DB::get("SELECT `name` FROM affiliates WHERE affiliate_id=%d", $id);
is harder to read or debug than the equivalent PDO. Typing out a bunch of unnecessary and redundant code is actually a pretty big burden if you have to do it every single time you need to run some SQL. Plus, it makes your functions huge and messy if you need to hit the database a few times in one function.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

fletcher posted:

If all you ever have to write are the simplest queries conceivable, that may be sufficient. Unfortunately the real world does not work this way. Utility functions like that do have their place once in awhile, but in my experience it's fairly limited.
That was an example, not an upper bound on what a wrapper function can do. You can use as complex a query as you'd like; for my big ones I use a heredoc to write them out readably before the method call. The point is to hide the calls that you would otherwise have to make every time you want to do a certain kind of lookup. To get one field in one row of a mysqli result set, with no outside assistance, you call mysqli_query, mysqli_fetch_assoc, and then separately get the index in the array that you want.

Those are things that have to be done every time you want one field. It doesn't help anyone to see them done yet again; why not made your code more concise without sacrificing readability by hiding them in a method that does it for you? Just because the query gets more complex doesn't mean the scaffolding you have to put around it to get useful data back does- most of the time you want a field, or a row, or a result set back, and helper functions make it easy on you.

MrMoo posted:

You should be using prepared statements, even better use stored procedures.
There's nothing wrong with prepared statements, but people go around treating them like a silver bullet and they aren't. As far as I'm concerned they don't offer any tangible benefits over using a sprintf-lite helper function, they certainly aren't more concise, and if we really needed the middling boost in performance from them I would probably just rewrite my helper functions to use them instead.

Stored procedures are nice for when you have one query you use in a lot of different places, but at my job that's the case for exactly one query that I can think of, and I can't even imagine what a royal pain it would be to store several thousand queries and update our codebase accordingly.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

VerySolidSnake posted:

I'm trying to make an automated installer / configuration utility for a framework and I've hit a snag. The configuration file is one giant array. Creating the array is no problem, however creating a file in it's place (using something like fwrite) with that array in a usable form has be stuck. Any ideas? Serialization is unfortunately not an option.

Are you looking for something like var_export()?

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

the php manual posted:

var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

OriginalPseudonym posted:

Is there any functional difference between var_export and serialize()? Just asking out of curiousity, since the latter is what I typically go to for stuff like this.

serialize() returns a string that can be unserialize()'d later to get the same value. var_export() doesn't need an unserializing function because it returns actual PHP code, and you use it when you want to do something like have a PHP script that generates another PHP script. Using either function can be a sign that you're about to do something gross, especially if you're sticking a serialized object into a database or using var_export() in tandem with eval().

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

the_cow_fan posted:

Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony.

It's hard to say what the "best framework" is. Asking people that question is probably going to get similar results as asking "What framework are you most familiar with?"

Give some thought to whether you really need a framework. PHP is actually pretty good on its own, and its frameworks tend to be big, lumbering, heavyweight things that force you into using their style. Rasmus Lerdorf (the dude who created PHP) is often critical of the larger frameworks, and wrote this article on a "no-framework MVC framework" to illustrate how you can write clean, well-organized code without using an external framework.

Personally, when I do need to use a framework, I like the Zend Framework. It's pretty good about letting you use only the parts you need.

e: apparently that link is down, here's google's cache in the meantime

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
Sure, if you're new to PHP and/or you're doing something that's already been done to death and/or performance isn't a concern, there's nothing wrong with using a framework. I'm just saying that a lot of developers consider copying over their MVC framework of choice to be the immutable step 0 of every project, but using a framework and using an MVC architecture are not only not prerequisites to writing good code but sometimes actively get in the way of getting things done.

VerySolidSnake posted:

edit: Also, didn't Rasmus argue against Object Oriented code at one point, and instead wrote a whole blog post about the benefits of procedural instead? He really isn't a good source of information.

I don't ever remember reading this, but I'm curious as to why you think it would discredit him. OOP isn't the one true way of doing things, it's just a paradigm with its own set of issues.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

Experto Crede posted:

At work, I run a unix command using watch to view the queue on our PBX, ideally I'd like to find a way to show this in a browser also in real time.

Is there a way in PHP to basically to view the live output of a command through shell_exec?

You can use popen() instead of shell_exec(), which will give you a stream you can read from just like a file.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
Does anyone know a way to get the SSH2 functions to connect via a proxy? Or any SSH library that will let me connect via a proxy? The only other thing I can think of doing is using proc_open('proxychains ssh...') but I really don't want to wrangle the input/output pipes if I can help it.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
Laravel is about to make me lose my poo poo. I've got these two Eloquent models, Thing and ThingBag. Thing has a BelongsTo relationship with ThingBag, and ThingBag has a HasMany relationship with Thing.

So I iterate over the Things in the ThingBag and do some work with them. I may need to remove a Thing from the ThingBag, which I do with:

code:
    $thing->myBag()->dissociate();
    $thing->push();
The problem is that doesn't update the ThingBag's Collection of Things--after the work is done, the page dumps out a list of the ThingBag's contents and the Thing will still be listed, even though they're no longer associated in the database. Okay, maybe I just need to update the ThingBag's too. But HasMany doesn't have a dissociate(), or any equivalent to it, as far as I can tell. I can do this:

code:
$thingbag->things = $thingbag->things->filter(function($v) use ($thething) {
    return $v->thing_id != $thething->thing_id;
});                                                                                                   
Which is hideous, but it does exactly what I want. Except the problem there is that Laravel thinks I'm trying to set a "things" property, not just set the relation defined by the "things()" method. So if I try to save the ThingBag after this, it tries to write a serialized string to the non-existent "things" column and barfs. HasMany::saveMany() appears to merge the Collection you give it with what the model already has, rather than replacing it, so if I filter the Collection and give it to saveMany() nothing actually happens.

This has got to be a common enough problem that I've just overlooked something in Laravel's vast, comprehensive documentation, right?

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

Depressing Box posted:

You can call $thingbag->load('things') to refresh the model's things relationship from the database.

Yeah, but I'd prefer not to hit the database again for something I already know.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

Depressing Box posted:

Ok, then instead of assigning directly to $thingbag->things, try:

PHP code:
$thingbag->setRelation('things', $filteredThings);

That's exactly what I needed, thanks a lot.

Adbot
ADBOT LOVES YOU

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
That's a hideous table structure. Really bad.

Anyway here's a rough idea of how to do what you want, assuming that each of your 136 attribute columns default to null and don't need to be specified unless they have a value. Good luck!

php:
<?php

foreach ($json_documents as $document) {
    $row = [
        'item_id'           => $document['item_id'],
        'name'              => $document['name'],
        // set other top-level elements
    ];

    $attr_count 1;
    foreach ($document['attributes'] as $attr) {
        $row["attribute{$attr_count}_id"] = $attr['attribute_id'];
        $row["attribute{$attr_count}_value"] = $attr['value'];
        $attr_count++;
    }

    do_insert($row);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply